如何重定向到另一个网页? [英] How do I redirect to another webpage?
问题描述
如何使用 jQuery 或纯 JavaScript 将用户从一个页面重定向到另一个页面?
How can I redirect the user from one page to another using jQuery or pure JavaScript?
推荐答案
不仅仅是使用 jQuery 重定向
jQuery 不是必需的,并且 window.location.replace(...)
将最好地模拟 HTTP 重定向.
One does not simply redirect using jQuery
jQuery is not necessary, and window.location.replace(...)
will best simulate an HTTP redirect.
window.location.replace(...)
比使用 window.location.href
好,因为 replace()
没有将原始页面保留在会话历史记录中,这意味着用户不会陷入无休止的后退按钮惨败中.
window.location.replace(...)
is better than using window.location.href
, because replace()
does not keep the originating page in the session history, meaning the user won't get stuck in a never-ending back-button fiasco.
如果您想模拟某人点击链接,请使用location.href
If you want to simulate someone clicking on a link, use
location.href
如果要模拟 HTTP 重定向,请使用 location.replace
If you want to simulate an HTTP redirect, use location.replace
例如:
// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");
// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";
这篇关于如何重定向到另一个网页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!