Last active
November 28, 2024 08:18
-
-
Save frontenddeveloping/7577049 to your computer and use it in GitHub Desktop.
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…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.