温馨提示×

js怎么实现页面跳转

js
小亿
69
2024-01-19 01:32:09
栏目: 编程语言

在JavaScript中,可以使用window.location对象来实现页面跳转。window.location对象包含了当前页面的URL信息,可以用来改变页面的URL来实现跳转。

以下是几种常见的页面跳转方法:

  1. 使用window.location.href属性来修改当前页面的URL,实现跳转:
window.location.href = "http://www.example.com"; // 跳转到指定的URL
  1. 使用window.location.replace()方法来修改当前页面的URL,实现跳转。与window.location.href相比,replace()方法在跳转后不会在浏览器的历史记录中生成新的记录。
window.location.replace("http://www.example.com"); // 跳转到指定的URL
  1. 使用window.location.assign()方法来修改当前页面的URL,实现跳转。与window.location.href相比,assign()方法在跳转后会在浏览器的历史记录中生成新的记录。
window.location.assign("http://www.example.com"); // 跳转到指定的URL

需要注意的是,页面跳转的URL可以是相对路径或绝对路径。如果是相对路径,则相对于当前页面的URL进行跳转。

0