JavaScript 提供了多种进行页面跳转的方法:直接修改 URL:window.location.href。替换历史记录:window.location.replace()。打开新窗口或标签页:window.open()。通过 DOM 修改 URL:document.location.href。通过 DOM 替换 URL:document.location.replace()。
如何使用 JavaScript 进行页面跳转
JavaScript 提供了多种方式来进行页面跳转,包括:
1. window.location.href
语法:window.location.href = “new_url”
此方法直接修改当前页面的 URL,从而导致页面跳转。
2. window.location.replace()
语法:window.location.replace(“new_url”)
此方法替换当前页面的历史记录,从而导致页面跳转。与 window.location.href 不同的是,它不会在浏览器历史记录中留下当前页面。
3. window.open()
语法:window.open(“new_url”)
此方法打开一个新窗口或标签页并加载指定 URL。
4. document.location.href
语法:document.location.href = “new_url”
此方法与 window.location.href 类似,但它通过 DOM 而不是 window 对象访问页面 URL。
5. document.location.replace()
语法:document.location.replace(“new_url”)
此方法与 window.location.replace() 类似,但它通过 DOM 而不是 window 对象替换页面 URL。
示例:
// 使用 window.location.href 跳转到新页面window.location.href = "https://www.example.com";// 使用 window.location.replace() 跳转到新页面window.location.replace("https://www.example.com");// 使用 window.open() 打开新窗口window.open("https://www.example.com");// 使用 document.location.href 跳转到新页面document.location.href = "https://www.example.com";// 使用 document.location.replace() 替换当前页面 URLdocument.location.replace("https://www.example.com");
登录后复制
以上就是js如何进行页面跳转的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2646740.html