How to use location.href?

“location.href is a property used to get or set the URL of the current page. By setting this property, you can implement page redirection, such as redirecting to a different page, or by getting the location.href property, you can obtain the URL of the current page.”

Here are the instructions for use:

  1. Get the current page’s URL.
var currentURL = location.href;
console.log(currentURL); // 输出当前页面的URL
  1. Page redirection:
location.href = 'https://www.example.com'; // 将页面重定向到指定URL

It is important to note that using the location.href property to achieve page redirection will result in the page refreshing, which may cause some page states to be lost when navigating to a new page.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds