js獲取當前url

來源:趣味經驗館 2.16W

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
熱門標籤