天天看點

js擷取目前頁面的url

設定或擷取對象指定的檔案名或路徑。

<script>

alert( window.location .pathname)

</script> 

設定或擷取整個 URL 為字元串。

<script> 

alert( window.location .href);

</script>

設定或擷取與 URL 關聯的端口号碼。

<script>

alert( window.location .port)

</script> 

設定或擷取 URL 的協定部分。

<script>

alert(window.location.protocol)

</script> 

設定或擷取 href 屬性中在井号“#”後面的分段。

<script>

alert(window.location.hash)

</script> 

設定或擷取 location 或 URL 的 hostname 和 port 号碼。

<script>

alert(window.location.host)

</script> 

設定或擷取 href 屬性中跟在問号後面的部分。

<script>

alert(window.location.search)

</script>

  1. window.location.host; //傳回url 的主機部分,例如:www.xxx.com 
  2. window.location.hostname; //傳回www.xxx.com 
  3. window.location.href; //傳回整個url字元串(在浏覽器中就是完整的位址欄),例如:www.xxx.com/index.php?class_id=3&id=2 
  4. window.location.pathname; //傳回/a/index.php或者/index.php 
  5. window.location.protocol; //傳回url 的協定部分,例如: http:,ftp:,maito:等等。 
  6. window.location.port //url 的端口部分,如果采用預設的80端口,那麼傳回值并不是預設的80而是空字元 

繼續閱讀