如何设置 javascript 页面跳转?使用 window.location 对象:设置 href、assign 或 replace 属性来直接跳转、无历史记录跳转或替换当前页面。使用 document.location 对象:与 window.location 类似,但适用于框架或 iframe 页面。使用 window.open 方法:在新窗口或标签页中打开 url。使用 location.reload() 方法:重新加载当前页面,忽略缓存数据。使用 history.back()/hist

(图1)

如何设置 JavaScript 页面跳转

1. window.location

window.location 对象提供了一系列属性和方法来控制页面跳转。

  • window.location.href = "new_url";:直接将页面跳转到指定 URL。
  • window.location.assign("new_url");:与 href 等价,但不会添加新条目到浏览器的历史记录中。
  • window.location.replace("new_url");:替换当前历史记录条目,将新页面加载到当前页面之上。

2. document.location

document.location 对象与 window.location 类似,但它适用于基于框架或 iframe 的页面。

3. window.open

window.open("new_url"); 方法在一个新窗口或标签页中打开指定的 URL。

4. location.reload()

location.reload(); 方法重新加载当前页面,忽略任何缓存数据。

5. history.back()/history.forward()

history.back(); 和 history.forward(); 方法可让用户在浏览器的历史记录中向前或向后导航。

实例:

// 将页面跳转到 "https://example.com"
window.location.href = "https://example.com";

// 在新窗口中打开 "https://example.com"
window.open("https://example.com");

// 重新加载当前页面
location.reload();

// 在浏览器历史记录中后退一步
history.back();
1、本站目前拥有近 1000+ 精品收费资源,现在加入VIP会员即可全部下载。
2、本资源部分来源其他付费资源平台或互联网收集,如有侵权请联系及时处理。
SEA模板网 » js跳转页面怎么设置

发表评论

加入本站VIP会员订阅计划,海量资源免费查看

目前为止共有 3654 位优秀的VIP会员加入! 立刻加入VIP会员