mark-in-motion
mark-in-motion

Reputation: 293

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

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.

Upvotes: 2

Views: 9563

Answers (4)

Vladislav Qulin
Vladislav Qulin

Reputation: 1942

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);
    });
}

Upvotes: 2

Roland Mai
Roland Mai

Reputation: 31097

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/

Upvotes: 2

Scary Wombat
Scary Wombat

Reputation: 44854

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

Upvotes: 0

Janith Chinthana
Janith Chinthana

Reputation: 3844

use window.open javascript function as bellow,

window.open("your link")

Upvotes: 0

Related Questions