Javascript window.open在Firefox中的奇怪行为

时间:2022-10-22 15:26:01

I have a few links that should all open in the same window or tab. To accomplish this I've given the window a name like in this example code:

我有一些链接应该在同一窗口或选项卡中打开。为了实现这一点,我给了窗口一个像这个示例代码中的名称:

<a href="#" onClick='window.open("http://somesite.com", "mywindow", "");'>link 1</a>
<a href="#" onClick='window.open("http://someothersite.com", "mywindow", "");'>link 2</a>

This works OK in internet explorer, but firefox always opens a new tab/window. Any ideas?

这在Internet Explorer中工作正常,但firefox总是打开一个新的选项卡/窗口。有任何想法吗?

4 个解决方案

#1


8  

The window.open() function in Javascript is specifically designed to open a new window, please see: w3schools documentation. It actually sounds like IE is handling things in a non-standard way (which is hardly surprising).

Javascript中的window.open()函数专门用于打开一个新窗口,请参阅:w3schools文档。实际上听起来IE正在以非标准的方式处理事情(这并不奇怪)。

If you want to relocate the existing location to a new page using Javascript, you should look at the location.replace() function.

如果要使用Javascript将现有位置重定位到新页面,则应查看location.replace()函数。

Generally speaking I would recommend that you develop for Firefox and then fix for IE. Not only does Firefox offer better development tools but its implementation of W3C standards tends to be more correct.

一般来说,我建议您为Firefox开发,然后修复IE。 Firefox不仅提供了更好的开发工具,而且其W3C标准的实现往往更加正确。

#2


14  

Actually, the W3Schools documentation linked to by gabriel1836 is only a very very brief summary of functions.

实际上,由gabriel1836链接的W3Schools文档只是功能的一个非常简短的摘要。

And Oddly, mozilla's own developer reference CONTRADITCS this logic.

而且奇怪的是,mozilla自己的开发人员参考了CONTRADITCS这个逻辑。

MDC / DOM / Window.open

MDC / DOM / Window.open

var WindowObjectReference = window.open(strUrl, 
              strWindowName [, strWindowFeatures]); 

If a window with the name strWindowName already exists, then, instead of opening a new window, strUrl is loaded into the existing window. In this case the return value of the method is the existing window and strWindowFeatures is ignored. Providing an empty string for strUrl is a way to get a reference to an open window by its name without changing the window's location. If you want to open a new window on every call of window.open(), you should use the special value _blank for strWindowName.

如果已存在名为strWindowName的窗口,则不会打开新窗口,而是将strUrl加载到现有窗口中。在这种情况下,方法的返回值是现有窗口,并忽略strWindowFeatures。为strUrl提供空字符串是一种通过名称获取对打开窗口的引用而无需更改窗口位置的方法。如果要在每次调用window.open()时打开一个新窗口,则应为strWindowName使用特殊值_blank。

However, the page also states that there are many extensions that can be installed that change this behaviour.

但是,该页面还指出可以安装许多可以更改此行为的扩展。

So either the documentation mozilla provides for people targeting their own browser is wrong or something is odd with your test system :)

因此,mozilla提供的文档是针对他们自己的浏览器的人是错误的,或者您的测试系统有些奇怪:)

Also, your current A-Href notation is bad for the web, and will infuriate users.

此外,您当前的A-Href表示法对网络不利,并会激怒用户。

  <a href="http://google.com"  
     onclick="window.open( this.href, 'windowName' ); return false" >
     Text
  </a>

Is a SUBSTANTIALLY better way to do it.

这是一个非常好的方法。

Many people will instinctively 'middle click' links they want to manually open in a new tab, and having your only href as "#" infuriates them to depravity.

许多人本能地“中间点击”他们想要在新标签中手动打开的链接,并且让你唯一的href作为“#”激怒他们堕落。

The "#" trick is a redundant and somewhat bad trick to stop the page going somewhere unintended, but this is only because of the lack of understanding of how to use onclick

“#”技巧是一个冗余而且有点糟糕的技巧,可以阻止页面无意中进入某个地方,但这只是因为对如何使用onclick缺乏了解

If you return FALSE from an on-click event, it will cancel the links default action ( the default action being navigating the current page away )

如果从点击事件返回FALSE,它将取消链接默认操作(默认操作是导航当前页面)

Even better than this notation would be to use unintrusive javascript like so:

甚至比这个表示法更好的是使用像这样的非侵入式javascript:

 <a href="google.com" rel="external" >Text</a>

and later

 <script type="text/javascript">
 jQuery(function($){ 
        $("a[rel*=external]").click(function(){ 
            window.open(this.href, 'newWindowName' ); 
            return false; 
        });
 }); 
 </script>

#3


6  

By default FF uses a new tab if dimensions are omitted on the window.open parameters. You need to add dimensions for a new browser window.

默认情况下,如果在window.open参数上省略了维度,FF将使用新选项卡。您需要为新的浏览器窗口添加维度。

Try this:

<a href="#" onClick='window.open("http://somesite.com", "mywindow", "width=700", "height=500");'>link 1</a>

#4


2  

Why don't you just use plain HTML like

你为什么不只使用简单的HTML

<a href="http://www.google.com" target="my_window_tab_frame_or_iframe">Link</a>

链接

instead of using JavaScript?

而不是使用JavaScript?

#1


8  

The window.open() function in Javascript is specifically designed to open a new window, please see: w3schools documentation. It actually sounds like IE is handling things in a non-standard way (which is hardly surprising).

Javascript中的window.open()函数专门用于打开一个新窗口,请参阅:w3schools文档。实际上听起来IE正在以非标准的方式处理事情(这并不奇怪)。

If you want to relocate the existing location to a new page using Javascript, you should look at the location.replace() function.

如果要使用Javascript将现有位置重定位到新页面,则应查看location.replace()函数。

Generally speaking I would recommend that you develop for Firefox and then fix for IE. Not only does Firefox offer better development tools but its implementation of W3C standards tends to be more correct.

一般来说,我建议您为Firefox开发,然后修复IE。 Firefox不仅提供了更好的开发工具,而且其W3C标准的实现往往更加正确。

#2


14  

Actually, the W3Schools documentation linked to by gabriel1836 is only a very very brief summary of functions.

实际上,由gabriel1836链接的W3Schools文档只是功能的一个非常简短的摘要。

And Oddly, mozilla's own developer reference CONTRADITCS this logic.

而且奇怪的是,mozilla自己的开发人员参考了CONTRADITCS这个逻辑。

MDC / DOM / Window.open

MDC / DOM / Window.open

var WindowObjectReference = window.open(strUrl, 
              strWindowName [, strWindowFeatures]); 

If a window with the name strWindowName already exists, then, instead of opening a new window, strUrl is loaded into the existing window. In this case the return value of the method is the existing window and strWindowFeatures is ignored. Providing an empty string for strUrl is a way to get a reference to an open window by its name without changing the window's location. If you want to open a new window on every call of window.open(), you should use the special value _blank for strWindowName.

如果已存在名为strWindowName的窗口,则不会打开新窗口,而是将strUrl加载到现有窗口中。在这种情况下,方法的返回值是现有窗口,并忽略strWindowFeatures。为strUrl提供空字符串是一种通过名称获取对打开窗口的引用而无需更改窗口位置的方法。如果要在每次调用window.open()时打开一个新窗口,则应为strWindowName使用特殊值_blank。

However, the page also states that there are many extensions that can be installed that change this behaviour.

但是,该页面还指出可以安装许多可以更改此行为的扩展。

So either the documentation mozilla provides for people targeting their own browser is wrong or something is odd with your test system :)

因此,mozilla提供的文档是针对他们自己的浏览器的人是错误的,或者您的测试系统有些奇怪:)

Also, your current A-Href notation is bad for the web, and will infuriate users.

此外,您当前的A-Href表示法对网络不利,并会激怒用户。

  <a href="http://google.com"  
     onclick="window.open( this.href, 'windowName' ); return false" >
     Text
  </a>

Is a SUBSTANTIALLY better way to do it.

这是一个非常好的方法。

Many people will instinctively 'middle click' links they want to manually open in a new tab, and having your only href as "#" infuriates them to depravity.

许多人本能地“中间点击”他们想要在新标签中手动打开的链接,并且让你唯一的href作为“#”激怒他们堕落。

The "#" trick is a redundant and somewhat bad trick to stop the page going somewhere unintended, but this is only because of the lack of understanding of how to use onclick

“#”技巧是一个冗余而且有点糟糕的技巧,可以阻止页面无意中进入某个地方,但这只是因为对如何使用onclick缺乏了解

If you return FALSE from an on-click event, it will cancel the links default action ( the default action being navigating the current page away )

如果从点击事件返回FALSE,它将取消链接默认操作(默认操作是导航当前页面)

Even better than this notation would be to use unintrusive javascript like so:

甚至比这个表示法更好的是使用像这样的非侵入式javascript:

 <a href="google.com" rel="external" >Text</a>

and later

 <script type="text/javascript">
 jQuery(function($){ 
        $("a[rel*=external]").click(function(){ 
            window.open(this.href, 'newWindowName' ); 
            return false; 
        });
 }); 
 </script>

#3


6  

By default FF uses a new tab if dimensions are omitted on the window.open parameters. You need to add dimensions for a new browser window.

默认情况下,如果在window.open参数上省略了维度,FF将使用新选项卡。您需要为新的浏览器窗口添加维度。

Try this:

<a href="#" onClick='window.open("http://somesite.com", "mywindow", "width=700", "height=500");'>link 1</a>

#4


2  

Why don't you just use plain HTML like

你为什么不只使用简单的HTML

<a href="http://www.google.com" target="my_window_tab_frame_or_iframe">Link</a>

链接

instead of using JavaScript?

而不是使用JavaScript?