如何使用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

THE END
Copyright © 2024 亿华云