开发者

JavaScript: 2 window.opener.location.href statements with alert() in between not functioning

开发者 https://www.devze.com 2023-01-02 10:38 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号