js中location.href的用法有哪些
在JavaScript中,location.href是一个属性,用于获取或设置当前页面的URL。它有以下用法:
获取当前页面的URL:
var url = location.href;
console.log(url);
设置当前页面的URL:
location.href = "http://example.com";
使用相对路径设置当前页面的URL:
location.href = "/path/to/page.html";
使用hash设置当前页面的URL:
location.href = "#section1";
使用查询参数设置当前页面的URL:
location.href = "?key=value";
使用hash和查询参数设置当前页面的URL:
location.href = "#section1?key=value";
将当前页面重定向到另一个URL:
location.href = "http://example.com";
请注意,使用location.href设置URL会导致页面的重加载。
阅读剩余
THE END