如何使用 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
此方法与 window.location.href 类似,但它通过 DOM 而不是 window 对象访问页面 URL。
5. document.location.replace()
语法:document.location.replace("new_url")
此方法与 window.location.replace() 类似,但它通过 DOM 而不是 window 对象替换页面 URL。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 使用 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中文网其它相关文章!