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.
Asked
Active
Viewed 2,761 times
0
-
You cannot control opening in new window vs new tab from javascript. It depends on browser settings and you can't control it – hop Mar 29 '13 at 15:54
-
Possible duplicate of http://stackoverflow.com/questions/4907843/open-url-in-new-tab-using-javascript – hop Mar 29 '13 at 15:56
4 Answers
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');
}

fmsf
- 36,317
- 49
- 147
- 195
-
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. – Halcyon Mar 29 '13 at 15:50
-
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.

Halcyon
- 57,230
- 10
- 89
- 128
-
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 – Chathula Mar 29 '13 at 15:59
-
1
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>

Matthew Poulson
- 3
- 3
-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.

Nelson
- 49,283
- 8
- 68
- 81
-
1Unless 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. – Simon Adcock Mar 29 '13 at 16:05
-
This is new behaviour that is starting to be implemented in browsers, not just the plain old behaviour your are thinking about. – Nelson Mar 29 '13 at 16:08
-
2Interesting, I didn't know that :) Do you have a link to any documentation? – Simon Adcock Mar 29 '13 at 16:14