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'</