location.href的使用有哪些方法
location.href是一个属性,用于获得或设置当前页面的URL。通过设置location.href属性,可以实现页面的跳转功能。例如,可以通过设置location.href属性来实现页面的重定向,还是通过获得location.href属性来获得当前页面的URL。
使用方法以下:
var currentURL = location.href;
console.log(currentURL); // 输出当前页面的URL
location.href = 'https://www.example.com'; // 将页面重定向到指定URL
需要注意的是,通过设置location.href属性来实现页面跳转时,会致使页面的刷新,所以在跳转页面时可能会致使部份页面状态的丢失。
TOP