I'm trying to open a new tab with javascript after the HTML is fully loaded by the browser. I tried this, but it's not working at all:
<script type="text/javascript">document.addEventListener("DOMContentLoaded",function(event){window.open('http://www.google.com');});</script>
Can you help me out and point me in the right direction?
Thanks!
You can just set a function as the window.onload
value:
window.onload = function() {
window.open('http://www.google.com')
}
The DOMContentLoaded event is fired when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading.
use load
event
<script> document.addEventListener("DOMContentLoaded", load, false); function load(){ window.open("https://www.w3schools.com", '_blank'); } </script>
Add _blank
if you need a new tab. And if you facing problems because of the browser, this link might help Open a URL in a new tab (and not a new window) . Hope this helps.
AutoExecutaable Functions In autoexecutable functions you dont need events or mouse event to execute the functions because the browser execute the function automatically, so do that:
(function(w){
w.open('http://www.google.com');
//or you can use the following instead window.open
w.location.href ="http://google.com";
})(window);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.