1、获取当前页面完整的url:
let url = location.href;
console.log(url) // --angel的博客_CSDN博客-领域博主
2、 获取当前页面的域名:
let host1 = window.location.host;
let host2 = document.domain;
let host3 = location.hostname;
console.log(host1, host2, host3) // mp.csdn.net
3、获取端口号:
let port = location.port;
console.log(port)
4、获取协议:
let protocol =location.protocol;
console.log(protocol) // https:
5、获取页面路径:
let pathName = location.pathname;
console.log(pathName) // /mp_blog/manage/article
6、获取href属性?后面的内容:
let str =location.search;
let search =a.substr(1);
console.log(search); // spm=1001.2100.3001.5448
7、获取=后面的内容:
let href = location.href;
let hrefcontent = href.substring(href.lastIndexOf('=')+1);文章来源:https://www.toymoban.com/news/detail-636015.html
console.log(hrefcontent) // 1001.2100.3001.5448文章来源地址https://www.toymoban.com/news/detail-636015.html
到了这里,关于JS获取URL信息的常用方法的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!