如何使用 javascript 获取 url

在 Web 应用程序中,经常需要访问当前页面的 URL。JavaScript 提供了多种方法来获取 URL。

使用 window.location 对象

window.location 对象提供了有关当前 URL 的详细信息。以下是如何使用它:

// 获取完整 URL
const url = window.location.href;

// 获取 URL 协议
const protocol = window.location.protocol;

// 获取域名
const hostname = window.location.hostname;

// 获取路径名
const pathname = window.location.pathname;

// 获取搜索参数
const search = window.location.search;

// 获取哈希值
const hash = window.location.hash;
1、本站目前拥有近 1000+ 精品收费资源,现在加入VIP会员即可全部下载。
2、本资源部分来源其他付费资源平台或互联网收集,如有侵权请联系及时处理。
SEA模板网 » 怎么用js获取url

发表评论

加入本站VIP会员订阅计划,海量资源免费查看

目前为止共有 3654 位优秀的VIP会员加入! 立刻加入VIP会员