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.
-
You cannot control opening in new window vs new tab from javascript. It depends on browser settings and you can't control ithop– hop2013-03-29 15:54:46 +00:00Commented Mar 29, 2013 at 15:54
-
Possible duplicate of stackoverflow.com/questions/4907843/…hop– hop2013-03-29 15:56:10 +00:00Commented Mar 29, 2013 at 15:56
4 Answers
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');
}
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.
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
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.