【问题标题】:How to open a new tab in javascript if it is not already open [duplicate]如果尚未打开,如何在javascript中打开新标签[重复]
【发布时间】:2018-09-22 04:52:49
【问题描述】:

我一直在寻找解决我的典型问题的方法,最后我找到了,但我需要让它更合适。 我的问题是我想在新的 tab 中打开一个链接,如果它还没有打开,我从 Mozilla doc 找到的例子正是我需要的,但它正在打开一个 中的新链接window 不在 tab 中有人可以帮我如何更改此代码以在新 tab 中打开链接,而不是 window

我的代码:

<script type="text/javascript">
var windowObjectReference = null; // global variable

function openFFPromotionPopup() {
  if(windowObjectReference == null || windowObjectReference.closed)
  /* if the pointer to the window object in memory does not exist
     or if such pointer exists but the window was closed */

  {
    windowObjectReference = window.open("https://jobscane.com/",
   "PromoteFirefoxWindowName", "resizable,scrollbars,status");
    /* then create it. The new window will be created and
       will be brought on top of any other window. */
  }
  else
  {
    windowObjectReference.focus();
    /* else the window reference must exist and the window
       is not closed; therefore, we can bring it back on top of any other
       window with the focus() method. There would be no need to re-create
       the window or to reload the referenced resource. */
  };
}
</script>

(...)

<p><a
 href="https://jobscane.com/"
 target="_blank"
 onclick="openFFPromotionPopup(); return false;" 
 title="This link will create a new window or will re-use an already opened one"
>Promote Firefox adoption</a></p>

【问题讨论】:

  • 这个功能不起作用至少有一个很好的理由——这样的代码可以用来强制标签在用户试图关闭它们时保持打开。为什么你不能只加载促销像其他人一样的 IFRAME?您甚至可以使用“灯箱”来显示 IFRAME,并使其看起来像在屏幕前弹出一样...... HTML5 有许多新功能,不会让用户无所适从。

标签: javascript mozilla


【解决方案1】:

要在新标签中打开,您必须使用旧的解决方法。

看到这篇文章:

Open a URL in a new tab (and not a new window) using JavaScript

【讨论】:

  • 如果您只是要链接到另一个 SO 问题,您应该投票关闭作为重复项。
【解决方案2】:

您可以使用Anchor 标签代替window.open()

尝试以下方法,看看它是否有效。此外,问题是行为可能因浏览器和用户设置而异。

<a href={link} target="_blank">Open in tab</a>

【讨论】:

    猜你喜欢
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    • 2019-08-17
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 1970-01-01
    • 2011-12-21
    相关资源
    最近更新 更多