js获取当前url

人气:381 ℃/2024-01-04 17:44:45

js 如何获取当前url?一起来看看吧!

url是因特网的万维网服务程序上用于指定信息位置的表示方法。

Javascript获取当前页的URL的函数就是我们经常用来重定向的window.location.href。

<script> var url=window.location.href; var loc = url.substring(url.lastIndexOf('/')+1, url.length);  alert("url="+url+",loc="+loc); </script>

利用window.location.href的变量获取整个url之后,要哪一部分的地址,利用substring,indexof等字符串处理函数对获取到的url进行处理,就可以截取你想要的部分。

另外列出一些 javascript 获取url中各个部分的功能方法:

返回url 的主机部分

window.location.host

返回www.xxx.com

window.location.hostname

返回/a/index.php或者/index.php

window.location.pathname

返回url 的协议部分

window.location.protocol

以上就是小编的分享,希望对大家有帮助。

<上一篇下一篇>

推荐

首页/电脑版/地图
© 2024 JiaChangBa.com All Rights Reserved.