js中location.href的用法有哪些
文章标签
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会导致页面的重加载。
版权声明
本文仅代表作者观点,不代表米安网络立场。
上一篇:excel指数函数怎么应用 下一篇:java对象的创建与使用方法是什么
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。