Internet Explorer 8 - setTimout 重定向到不同页面
我试图通过 javascript 在 1 秒后将用户重定向到不同的页面:
setTimout("document.location.href='new_page.html'", 1000);
但是,在 Internet Explorer 中,这种情况会立即发生,而不是 1 秒后。有什么想法吗?
I am trying to redirect the user to a different page after 1 second via javascript:
setTimout("document.location.href='new_page.html'", 1000);
however, in Internet Explorer, this happens immediately, not 1 second later. Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了几个小错误之外,您引用的内容应该有效:
您在 setTimeout 中缺少“e”
您正在使用
文档.location
;它应该是window.location
。刚刚在IE8上测试了一下,果然如期而至。您是否在某些会使页面重新加载的事件中执行此操作,例如表单的
submit
事件?如果是这样,您需要取消表单提交以避免取代您的setTimeout
代码。如何执行此操作取决于您如何挂钩事件(例如,如果您使用 DOM0onsubmit="..."
处理程序,请使用return false;
; 如果您使用的是更现代的东西,您需要event.preventDefault()
; 如果您使用的是 jQuery、Prototype 或其他一些库,请检查他们的文档以获取正确的方法来防止事件的默认操作)。现在,尽管它按照您的方式工作,但通常最好使用函数而不是字符串中的代码来执行此操作,例如:
但无论哪种方式都应该有效。
What you've quoted should work, except for a couple of minor errors:
You're missing the "e" in setTimeout
You're using
document.location
; it should bewindow.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 yoursetTimeout
code. How you do that will depend on how you're hooking the event (e.g., if you're using a DOM0onsubmit="..."
handler, usereturn false;
; if you're using something more modern, you wantevent.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.:
But either way should work.
将其包装在一个函数中。
请注意,如果您始终在页面加载时执行此操作,则实际上应该使用元刷新标记。
Wrap it in a function.
Note that, if you are always doing this on page load, you should really use the meta refresh tag instead.