Closing the window after full page load

I am trying to write a JavaScript function that will work in Firefox 5.0. I need the page to fully load and then close. I am trying to do this:

var temp = window.open(link);

temp.window.onload = function () {
    temp.window.close();
}

      

But for now, all it does is open a new tab but not close it.

Is there a way to accomplish this successfully?

+3


source to share


4 answers


Maybe you can create a new js file for this window and only have window.close (); inside.



+2


source


First, if the link is not in the same domain, you will not be able to close the window due to the same origin policy.

Listens for onload event using addEventListener

var temp = window.open(link); 
temp.addEventListener('load', function() { temp.close(); } , false);

      

if you need to support older IE than you would need attachEvent

var temp = window.open(link); 
temp[temp.addEventListener ? 'addEventListener' : 'attachEvent']( (temp.attachEvent ? 'on' : '') + 'load', function() { temp.close(); }, false );

      




There is no need to open a window to get to the web page.

You can:

  • Make Ajax request - must be the same domain
  • Set hidden iframe
  • Set image source
+3


source


If you have access to the popup, you can add this:

JQuery

<script>
    $(window).load(function(){
        open(location, '_self').close();
    });
</script>

      

Javascript

<script>
  window.onload = function () {
     open(location, '_self').close();
   };
</script>

      

I also suggest you read this Question and this answer

+2


source


you can use something like

function closed(){
 
 setTimeout("window.close()", 6000);
 }
 
 
 
      

Run codeHide result


0


source







All Articles