Force different links to open in a new Pop-up window every time

天大地大妈咪最大 提交于 2019-12-22 12:17:35

问题


I have an HTML page and have Four Links to go to Different pages on a new window.

For Example:

Link1, Link2, Link3 Link4

And I want Link 1 to pop-up in a new Window 1, Link2 to new Window 2 and so on...

So that Window 1 and its content from Link1 will stay when Link2 is clicked.

Currently, the problem is that the four links opens up in a new Window 1 covering up the first current content from Link1. What I want is to have four unique window every time a Link is clicked.

I don't know if there's a certain Javascript function to do this but what I have right now on those four link is just target="_blank" on an anchor tag. This happens in IE and Chrome and I would think also in FF and any other browsers.

Thanks in advance for the help.


回答1:


target="_blank" will open each link in a new window (it doesn't matter if the link is already opened);

If you want to focus the opened window, you can use modal windows (i.e. not tabs) as follows:

html:

<a href="http://google.com">link 1</a><br/>
<a href="http://yahoo.com">link 2</a><br/>
<a href="http://bing.com">link 3</a><br/>
<a href="http://mamma.com">link 4</a>

js:

$(document).ready(function() {
    $('a').click(function(e) {
        var id = $(this).data("windowid");
        if(id == null || id.closed) {
            id =  window.open($(this).attr("href"), '_blank', 'modal=yes');
        }
        id.focus();
        $(this).data("windowid", id);
        e.preventDefault();
        return false;
    });
});

http://jsfiddle.net/JeQyE/2/




回答2:


Try

var links = document.getElementsByTagName("a");
for (var i = 0; i < links.length; i++) {
    var link = links[i];
    link.addEventListener("click", function (e) {
        e.preventDefault();
        window.open(this.href);
    });
}



回答3:


use window.open javascript function as bellow,

window.open("your link")



回答4:


target can also accept a name instead of "_blank" e.g. win1 win2 etc

see http://www.w3.org/html/wg/drafts/html/master/browsers.html#browsing-context-names



来源:https://stackoverflow.com/questions/18373892/force-different-links-to-open-in-a-new-pop-up-window-every-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!