
正文
如何用JavaScript重定向到另一个网页?
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
可以使用 window.location.replace() 方法重定向到另一个页面。相对于 window.location.href ,replace的优势是不会跳转记录并不会保留在历史会话中,这就是说用户可以通过后退按钮回到上一个正常页面,而不是进入到无休止的回退→跳转的流程。
下面是对使用JavaScript来进行跳转的一些建议:
- 如果是想模拟用户点击链接进行跳转,建议使用window.location.href
- 如果是想模拟HTTP重定向,建议使用window.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";
来自Stack Overflow






