0

I need JavaScript code or HTML to make two websites open in two new browser tabs when clicking on one link. I do not want them to open in new windows, or on the current page that the link is on.

2
  • You cannot control opening in new window vs new tab from javascript. It depends on browser settings and you can't control it Commented Mar 29, 2013 at 15:54
  • Possible duplicate of stackoverflow.com/questions/4907843/… Commented Mar 29, 2013 at 15:56

4 Answers 4

3

It probably won't work because the browser might consider it a popup and block it.

If the user allows popups you can do:

window.open(url, '_blank');

Like:

<a id="mydoublelink" href="http://site1.com" target="_blank">foo</a>

document.getElementById("mydoublelink").onclick=function(){
    window.open('http://site2.com', '_blank');
}
Sign up to request clarification or add additional context in comments.

2 Comments

Most browser popup-blockers allow popups that are spawned from a click-event (and some others). So it will work unless you have a very aggressive popup-blocker.
@FritsvanCampen added "probably" to the sentence.
2

If you call window.open in the onclick event you should be fine. Built-in popup blockers allow those. The kind of popups that get blocked come from other events or from scheduled events like a setTimeout.

document.getElement("my_link").onclick = function () {
    window.open(/*..*/); // works
}

document.getElement("my_link").onclick = function () {
    setTimeout(function () {
        window.open(/*..*/); // will probably get blocked
    });
}

This means, for instance, that if you open a popup after an AJAX call it will very likely get blocked. A workaround in this case is to open the popup right away and fill in the content later. This is outside the scope of this question but I feel like this is information that everyone should know.

2 Comments

i want to open two links in onclick on the link..but i dont want to close the current tab..both link muse be opened on another two tabs
Then call window.open() twice
0

Something like this?

<!DOCTYPE html>
<html>
<head>
<script>
function open_win() 
{
window.open("URL");
open_win_two();
}
function open_win_two()
{
window.open("URL");
}
</script>
</head>
<body>
<a onclick="open_win()">luyfl</a>
</body>
</html>

Comments

-1

Yu can try the new target _newtab:

<a href="site.html" target="_newtab">blabla</a>

It works in Firefox, don't know if it's supported in other browsers.

3 Comments

Unless I've missed my guess, this will just open site.html in a new window that happens to be named "_newtab". Clicking on the link again will open the page again in the same window. Whether or not this is actually a new tab depends on how the user has set up their browser. It could just as easily open in a new browser window.
This is new behaviour that is starting to be implemented in browsers, not just the plain old behaviour your are thinking about.
Interesting, I didn't know that :) Do you have a link to any documentation?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.