views:

298

answers:

4

How to open 2 different link one in same window and another one in new window from one link? I want to open one link in parent window and other one in new window not new tab in all A-Grade browsers

+1  A: 

Use a javascript function that first calls window.open and then window.location.

Murali VP
`window.open` calls are often blocked by popup blockers, whereas `target=_blank` usually goes around it.
intgr
window.location is not needed if you just specify the redirect URL in the `href` attribute as you normally would.
Justin Johnson
A: 
<a href="another.htm" target="_blank" "onclick="location.href='one.htm'">Your link</a>

This may or may not work, depending on whether the 'onclick' handler runs before the standard behaviour of the link.

If it doesn't - or is intermittent - let me know, and I'll supply an alternative approach.

EDIT:

As an alternative, I'm thinking that you could have 2 links, one for the 'new' window and one for the 'current' window. Make the 'current' window link invisible, using css, amd add an 'onclick' handler to the 'new' link, that fires the 'current' link.

    <a href="another.htm" target="_blank" "onclick="$('#currentLink').click()">Your link</a>
    <a href="one.htm" id="currentLink" style="display: none">Hidden link</a>

Be sure to check this on multiple browsers.

P.S. I'm assuming that you're using jquery - if not, the code that triggers the 'click' event will need to change.

belugabob
onclick always runs first.
Caleb
Now that you mention it, it has to - doesn't it? In that case, an alternative, fully javascript (i.e. not relying on the default link behaviour) approach is necessary
belugabob
In Firefox both links opens in tab, in safari it's working fine as i wanted but in IE6 onclick link does not open. I want to open one link in parent window and other one in new window not new tab.
metal-gear-solid
Hmm, interesting - just goes to show that I haven't fel the need to use '_blank' since the advent of tabbed browsers. The behaviour of a brwaser, with respect to opening new windows, is not only dependent on the browser, but also the configuration chosen by the user. This makes it somewhat tricky to produce predictable behaviour for a given piece of markup/javascript. See my comment, on your question, about CSS and printing.
belugabob
Firefox, Chrome and Opera open the '_blank' page in a new tab, IE8 and Safari, open them in new windows.AS I said before, this can probably be changed in the browser settings, but this isn't too practical, when you've got loads of users with different settings.
belugabob
can we override user browser settings with through javascript
metal-gear-solid
Given there's no way of differentiating between opening a new tab vs new window in javascript, I'd say no, you can't force a window.
K Prime
You're using jQuery and the `onclick` attribute? Shameful.
Justin Johnson
Yes - bringing event binding (which I think you're hinting at) into the discussion would just cloud the waters somewhat. Once the actual mechanism is in place, the OP is free to 'optimise' things as he sees fit.
belugabob
A: 
<a onClick="window.open('http://something.com');window.location = 'http://somethingelse.com'"&gt;link&lt;/a&gt;
Brandon G
+1  A: 

Typically, if you use window.open and specify a height and width for the window it will cause most browsers with most configurations to open it as a new window and not a new tab.

The following will add a popup window to the link with the id link-of-doom. Specify the link that you want the current page to redirect to in the href attribute as you normally do.

HTML

<a href="/page1.html" id="link-of-doom">Click me!</a>

JavaScript

$(function() {
    $("#link-of-doom").click(function() {
        window.open('/page2.html', 'sometarget', 'width=400,height=200');
    });
});

* You should not use the onclick attribute in the HTML itself as it is not considered a best practice . . . and a kitten is killed every time someone uses it.

Justin Johnson