Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
My example can open many links in tabs, not in new windows. Why need this and do not use window.open? Because there is the problem - if you call once window.open it will open new tab or window(it depends of browser), if will call window.open two or more times - first will open like tab/window and all next in the new window only. The individual b…
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Open 3 tabs</title>
<script src="http://codeorigin.jquery.com/jquery-1.10.2.min.js"></script>
</head>
<body>
<div id="hidden_div"></div>
<script>
var urls = ['http://facebook.com','http://google.com','http://yahoo.com'],
isFF = navigator.userAgent.toLowerCase().indexOf('firefox') > 0,
isIE = /*@cc_on!@*/false,
hiddenArea = $('#hidden_div').hide();
function emulateClick(element){
var evt = element.ownerDocument.createEvent('MouseEvents');
//https://developer.mozilla.org/en-US/docs/Web/API/event.initMouseEvent
evt.initMouseEvent('click', true, true, element.ownerDocument.defaultView, 1, 0, 0, 0, 0, true, false, false, true, 0, null);
if (document.createEventObject){
element.fireEvent('onclick', evt)
} else{
element.dispatchEvent(evt);
}
}
if (isFF || isIE) {
$.each(urls, function (index, url) {
var $form = $('<form>', {
"target" : "_blank",
"action" : url,
"method" : "post",
"name" : "form" + index
});
hiddenArea.append($form);//need to be attached to DOM
$form.get(0).submit();
});
} else {
$.each(urls, function (index, url) {
var $link = $('<a>', {
"target" : "_blank",
"href" : url
});
emulateClick($link.get(0));
});
}
</script>
</body>
</html>
@frontenddeveloping

This comment has been minimized.

Copy link
Owner Author

@frontenddeveloping frontenddeveloping commented Nov 21, 2013

But don't rely on this solution which open new tabs in 100% cases. In some browser user can configure this option - open in new tab or window all links.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment