views:

6605

answers:

4

Hi,

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?

+6  A: 

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).

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

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.

Noah Goodrich
+1: "develop for Firefox and then fix for IE"
Kon
You have a point, but one can only say usually ... Because if you are going to implement your own DOM-handling FireFox allows a lot of crap !-)
roenving
I develop in Firefox, regularly test in Safari, Chrome, and Opera. Then cross my fingers and fix in IE. It's true that you can get away with too much in any single browser.
Nosredna
+5  A: 

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.

Try this:

<a href="#" onClick='window.open("http://somesite.com", "mywindow", "width=700", "height=500");'>link 1</a>
Diodeus
Thanks. I didn't know that.
Noah Goodrich
Thanks from me too! I was wondering why in IE it works as expected, but in FF - always a new tab...
m_pGladiator
+6  A: 

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

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

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.

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 :)

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

  <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.

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

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 )

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

 <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>
Kent Fredric
A: 

Why don't you just use plain HTML like

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

instead of using JavaScript?

Spidey