开发者

How to open 2 different link one in same window and another one in new window from one link?

How to open 2 different link one in sam开发者_Python百科e 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


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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜