所有的代码都是可用,而且附了图片的,不过是直接用我自己的文章地址,所以有些显示的有点奇怪。

大家可以找个网址试试代码是否可行。

 

console.log(window.location.pathname)

js得到当前页面的url信息

2,设置或获取整个 URL 为字符串。

console.log(window.location.href);

 

js得到当前页面的url信息

3,设置或获取与 URL 关联的端口号码。

console.log(window.location.port)

js得到当前页面的url信息

4,设置或获取 URL 的协议部分。

console.log(window.location.protocol)

js得到当前页面的url信息

5,设置或获取 href 属性中在井号“#”后面的分段。

console.log(window.location.hash)

js得到当前页面的url信息


6,设置或获取 location 或 URL 的 hostname 和 port 号码。

console.log(window.location.host)

js得到当前页面的url信息

7,设置或获取 href 属性中跟在问号后面的部分。

console.log(window.location.search)

js得到当前页面的url信息


8,获取变量的值(截取等号后面的部分)

 var url = window.location.search;
 console.log(JSON.stringify(url));
 console.log(url.length);
 console.log(url.lastIndexOf('='));
 var loc = url.substring(url.lastIndexOf('=')+1, url.length);

 console.log(loc);

js得到当前页面的url信息


9,用来得到当前网页的域名

console.log( document.domain);

js得到当前页面的url信息

 

相关文章:

  • 2021-08-10
  • 2022-02-02
  • 2022-03-08
  • 2021-07-28
  • 2021-09-21
  • 2021-05-16
  • 2021-12-16
猜你喜欢
  • 2021-09-18
  • 2022-12-23
  • 2021-08-31
相关资源
相似解决方案