我只想获取网站URL。不是从链接获取的URL。在页面加载时,我需要能够获取网站的完整、当前URL,并将其设置为一个变量,以便根据需要进行处理。
当前回答
let url=新url(window.location.href);console.log(url.href);
使用以上代码获取网站的当前URL。
或者试试这个https://bbbootstrap.com/code/get-current-url-javascript-54628697
其他回答
location.origin+location.pathname+location.search+location.hash;
and
location.href
也一样。
使用window.location对与当前帧关联的位置对象进行读写访问。如果您只想以只读字符串形式获取地址,可以使用document.URL,它应该包含与window.location.href相同的值。
添加结果以供快速参考
窗口位置;
Location {href: "https://stackoverflow.com/questions/1034621/get-the-current-url-with-javascript",
ancestorOrigins: DOMStringList,
origin: "https://stackoverflow.com",
replace: ƒ, assign: ƒ, …}
文档位置
Location {href: "https://stackoverflow.com/questions/1034621/get-the-current-url-with-javascript",
ancestorOrigins: DOMStringList,
origin: "https://stackoverflow.com",
replace: ƒ, assign: ƒ
, …}
窗口位置路径名
"/questions/1034621/get-the-current-url-with-javascript"
窗口.location.href
"https://stackoverflow.com/questions/1034621/get-the-current-url-with-javascript"
位置主机名
"stackoverflow.com"
要获取路径,可以使用:
http://www.example.com:8082/index.php#tab2?foo=789
Property Result
------------------------------------------
window.location.host www.example.com:8082
window.location.hostname www.example.com
window.location.port 8082
window.location.protocol http:
window.location.pathname index.php
window.location.href http://www.example.com:8082/index.php#tab2
window.location.hash #tab2
window.location.search ?foo=789
window.location.origin https://example.com
获取当前页面URL:
window.location.href
推荐文章
- 如何为Firebase构建云函数,以便从多个文件部署多个函数?
- 如何发送推送通知到web浏览器?
- AngularJS:工厂和服务?
- js:将一个组件包装成另一个组件
- 父ng-repeat从子ng-repeat的访问索引
- JSHint和jQuery: '$'没有定义
- 模仿JavaScript中的集合?
- 用JavaScript验证电话号码
- 如何在HTML5中改变视频的播放速度?
- 谷歌地图API v3:我可以setZoom后fitBounds?
- ES6/2015中的null安全属性访问(和条件赋值)
- 与push()相反;
- JS字符串“+”vs concat方法
- AngularJS使用ng-class切换类
- 访问Handlebars.js每次循环范围之外的变量