JavaScript: 2 window.opener.location.href statements with alert() in between not functioning
I need to make a little JS app to scroll automatically through a list of URLs. I've chosen to have the functionality in a pop-up, for various开发者_开发百科 reasons.
The syntax to change the opening window's URL is:
window.opener.location.href = "http://www.example.com";
This works fine with one URL, but if two statements are called, only one is executed. I experimented with an alert statement between two of the above statements, and the alert event made the second statement function properly:
window.opener.location.href = "http://www.example1.com";
alert("hello world");
window.opener.location.href = "http://www.example2.com";
Question is: does anyone know how to get the first and second window.opener statements to work, without the intervening alert();? Also, how can I add a pause between the two statements, so that the second executes a couple of seconds after the first?
Thanks so much!
You need to call setTimeout
, like this:
window.opener.location.href = "http://www.example1.com";
setTimeout(function() {
window.opener.location.href = "http://www.example1.com";
}, 5000); //5,000 milliseconds
Is the original location of the opener page also at http://www.example1.com
? If not, then when the first statement executes and it changes the location of the opener to a different domain, then the window.opener
property becomes not accessible -- hence the second statement fails.
精彩评论