-1

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!

Johnson
  • 9
  • 3

4 Answers4

0

You can just set a function as the window.onload value:

window.onload = function() {
  window.open('http://www.google.com')
}
csander
  • 1,385
  • 10
  • 11
0

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

ewwink
  • 18,382
  • 2
  • 44
  • 54
0

<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.

amrendra
  • 345
  • 2
  • 12
0

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);
Risa__B
  • 451
  • 4
  • 8