开发者

Internet Explorer 8 - setTimout redirect to different page

I am trying to redirect the user to a different page after 1 second via javascript:

setTimout("document.location.href='new_page.html'", 1000);

howe开发者_开发技巧ver, in Internet Explorer, this happens immediately, not 1 second later. Any thoughts?


What you've quoted should work, except for a couple of minor errors:

  1. You're missing the "e" in setTimeout

  2. You're using document.location; it should be window.location.

Just tested it on IE8 and it waited as expected. Are you doing this from within some event that would make the page reload anyway, like a form's submit event? If so, you'll need to cancel the form submission to avoid that superceding your setTimeout code. How you do that will depend on how you're hooking the event (e.g., if you're using a DOM0 onsubmit="..." handler, use return false;; if you're using something more modern, you want event.preventDefault(); if you're using jQuery, Prototype, or some other library, check their docs for the right way to prevent the default action of the event).

Now, although it works the way you did it, it's typically better to do this with a function rather than code within a string, e.g.:

setTimeout(function() {
    window.location.href = 'new_page.html';
}, 1000);

But either way should work.


Wrap it in a function.

setTimeout( function() { location.href = 'new_page.html'; }, 1000 );

Note that, if you are always doing this on page load, you should really use the meta refresh tag instead.

 <meta http-equiv="refresh" content="1;url=new_page.html">
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜