js如何进行页面跳转

来源:undefined 2025-02-13 04:05:57 1019

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

此方法与 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中文网其它相关文章!

最新文章