如何使用JavaScript重定向到另一个网页

本文详细介绍了使用JavaScript重定向到另一个网页的各种方法,包括使用History API的`pushState`、`replaceState`方法,以及设置`window.location`的`href`属性等。还讨论了301服务器端重定向的重要性,以及在某些情况下使用HTML元标记进行重定向的选择。
摘要由CSDN通过智能技术生成

JavaScript offers many ways to redirect the user to a different web page, if during the execution of your program you need to move to a different page.

JavaScript提供了许多将用户重定向到其他网页的方法,如果在执行程序期间您需要移动到其他页面。

The one that can be considered canonical to navigate to a new URL is

可以认为可以导航到新URL的规范是

window.location = 'https://newurl.com'

If you want to redirect to a different path, on the same domain, use:

如果要重定向到同一域上的其他路径,请使用:

window.location.pathname = '/new'

This is using the location object offered by the History API.

这使用的是History API提供的location对象。

其他重定向选项 (Other options to redirect)

As with most things in programming, there are many ways to perform the same operation.

与编程中的大多数事情一样,执行相同操作的方法有很多。

Since window is implicit in the browser, you can also do:

由于window在浏览器中是隐式的,因此您还可以执行以下操作:

location = 'https://newurl.com'</
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值