开发者

Set top.location and open new tab?

i'm using the following code:

$('a[href*="somelinktext"]').click(function(e) {
        e.preventDefault();
        top.loca开发者_Python百科tion = "http://www.example.com";
    });

Is it possible to open a new window/tab with this link-click?


For opening a new tab/window (depending on the user's browser settings), simply add the target attribute. To change the location of the top-level frame of the current tab, you do need an onclick handler though.

$('a[href*="somelinktext"]').each(function() {
    $(this).attr('target', '_blank')
}).click(function(e) {
    top.location = 'http://www.example.com';
});

But if you only want to open a new tab, just stay with the target attr:

$('a[href*="somelinktext"]').each(function() {
    $(this).attr('target', '_blank')
});

You could also set it in the HTML code itself though:

<a href="..." target="_blank">blah</a>

(it supports _top for the top level frame, too)


Use window.open rather than top.location?


I think You can use window.open function for that.

Thanks

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜