How to achieve page redirection in JavaScript?
In JavaScript, you can use the window.location object to achieve page redirection. This object holds the URL information of the current page and can be used to change the URL of the page for redirection purposes.
Here are some commonly used methods for page redirection:
- the URL of the current page
window.location.href = "http://www.example.com"; // 跳转到指定的URL
- Navigate to a new web page without saving the previous page in the browser’s history.
- The address of the current web page.
- Substitute
window.location.replace("http://www.example.com"); // 跳转到指定的URL
- Redirect the current page to a new location.
- the URL of the current window
- designate
window.location.assign("http://www.example.com"); // 跳转到指定的URL
It is important to note that the URL for page redirection can be either a relative path or an absolute path. If it is a relative path, it will redirect based on the current page’s URL.