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 acplish this?
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 acplish this?
Share Improve this question edited Nov 12, 2014 at 14:03 APerson 8,4228 gold badges38 silver badges49 bronze badges asked Nov 12, 2014 at 14:00 user3636583user3636583 1772 gold badges3 silver badges12 bronze badges 3- 1 Have you access to openning window code? I mean maybe it is better to put second part of your code to new window? – Boris Zagoruiko Commented Nov 12, 2014 at 14:04
- I'm a beginner at Javascript, can you please explain a bit more? – user3636583 Commented Nov 12, 2014 at 14:05
- 1 Is the link in a different domain or sub domain? – epascarello Commented Nov 12, 2014 at 14:17
4 Answers
Reset to default 6First 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);
}