JavaScript 提供了多种进行页面跳转的方法:直接修改 URL:window.location.href。替换历史记录:window.location.replace()。打开新窗口或标签页:window.open()。通过 DOM 修改 URL:document.location.href。通过 DOM 替换 URL:document.location.replace()。
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() 替换当前页面 URL document.location.replace("https://www.example.com");
以上就是js如何进行页面跳转的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号