如何使用js获取当前页面的url网址后续的内容

很多小伙伴问了使用js获取当前页面的url网址内容后后续的信息如何获取下面给大家介绍一下:

如何使用js获取当前页面的url网址后续的内容

 

一、获取当前页面的url网址内容代码如下:

//获取对象指定的路径。alert(window.location.pathname);//获取整个URL为字符串。alert(window.location.href);//获取与URL关联的端口码。alert(window.location.port);//获取URL的部分协议。alert(window.location.protocol);//获取href属性中在井号“#”后面的分段。alert(window.location.hash);//获取location或URL的hostname和port号码。alert(window.location.host);//设置或获取href属性中跟在问号后面的部分。alert(window.location.search);

二、获取url=后面的内容(只有一个参数)

let url = window.location.search;let neirong = url.substring(url.indexOf('=')+1, url.length);

三、获取url后面的参数(有多个参数) 把参数转成对象

// console.log(location.search)//http://127.0.0.1:5500/into.html?userName=111&password=222333&submit=%E6%8F%90%E4%BA%A4// 把问号去掉let removeMark=location.search.substr(1)//userName=111&password=222333&submit=%E6%8F%90%E4%BA%A4// console.log(removeMark)// 分割&  得到每组数据let group=removeMark.split('&')//["userName=111", "password=222333", "submit=%E6%8F%90%E4%BA%A4"]// console.log(group)let list=[]//最终得到的  里卖弄是对象的数组数据// 用map把每组数据变成对象let data=group.map((groupItem,groupIndex)=>{// 获取等号的下标let eq=groupItem.indexOf('=')// console.log(eq)// 获取键值对的键和值let pre=groupItem.slice(0,eq)let back=groupItem.slice(eq+1)// console.log(pre,back)// 拼接成对象let obj={}
    obj[pre]=back
    list.push(obj)})
console.log(list)//) [{userName: "111"},{password: "222333"},{submit: "%E6%8F%90%E4%BA%A4"}]//得到url的值for(var i=0;i<list.length;i++){
    list[i].password&&console.log(list[i].password)}

以上是关于“如何在react中定义并且使用全局变量”的介绍,如需购买云虚拟主机,推荐酷番云,共享虚拟主机、独享IP虚拟主机齐备,各类配置均有,满足不同网站建设需求;价格实惠;最低9元/月,提供免费备案,让您快速上线网站。

产品选购地址:https://www.kufanyun.com/host/buy.html

 

图片来源于AI模型,如侵权请联系管理员。作者:路飞,如若转载,请注明出处:https://www.kufanyun.com/ask/1147.html

(1)
上一篇2021年10月22日 15:35
下一篇 2021年10月25日 15:42

相关推荐

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注