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进行处理,就可以截取你想要的部分。

js获取当前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