简体   繁体   中英

Closing a window after the page is fully loaded

I am trying to write a JavaScript function that will work on Firefox 5.0. I need the page to fully load, and then close. What I'm trying to do is:

var temp = window.open(link);

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

But so far all it does is open the new tab, but doesn't close it.

Is there any way to successfully accomplish this?

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

Listens for the onload event with addEventListener

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

if you need to support old IEs than you would need to 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 up a window to hit a web page.

You can:

  • Make an Ajax request - must be same domain
  • Set a hidden iframe
  • Set an image source

You could maybe create new js file for that window and have just window.close(); inside.

If you have a access to the popup window 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

you can use something like

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM