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