3

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?

APerson
  • 8,140
  • 8
  • 35
  • 49
user3636583
  • 177
  • 1
  • 3
  • 11

4 Answers4

6

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
epascarello
  • 204,599
  • 20
  • 195
  • 236
2

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

Jure
  • 799
  • 6
  • 25
1

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

Community
  • 1
  • 1
Leysam Rosario
  • 379
  • 2
  • 11
1

you can use something like

function closed(){
    setTimeout("window.close()", 6000);
}
Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
betrice mpalanzi
  • 1,436
  • 12
  • 16