location 是 window 物件下的特徵,你可以用 window.location;也可以省略 window,直接用 location。
URL 中 # 之後的文字,包括 #。# 是用在 錨 的符號。
- 程式用法:
<script type='text/javascript'>
document.write( window.location.hash);
</script> - 執行結果:
主機名稱與港號。
主機名稱。
港號。
- 程式用法:
<script type='text/javascript'>
document.write( '主機:'+ location.host
+'<br />主機名稱:'+ location.hostname
+'<br />港號:'+ location.port);
</script> - 執行結果:
主機:ant4js.blogspot.com
主機名稱:ant4js.blogspot.com
港號:
完整的 URL。可設定此特徵新位址,下載新網頁。
- 程式用法:
<script type='text/javascript'>
document.write( location.href );
</script>
<p><a href='javascript:location.href="http://gamelily.blogspot.com/2008/11/blog-post_7559.html#sh"'>
射擊遊戲</a> - 執行結果:
http://ant4js.blogspot.com/2009/01/locationpty.html
相對於主機的檔案路徑名。
通信規則。例如 http:, https:, file:, ftp: 等等。
- 程式用法:
<script type='text/javascript'>
document.write( location.pathname
+'<br />'+ location.protocol );
</script> - 執行結果:
/2009/01/locationpty.html
http:
URL 中 ? 之後的文字,包括 ?。改變此值,會至主機作新的搜尋。
- 程式用法:
<script type='text/javascript'>
document.write( location.search );
</script>
<p><a href='javascript:location.search="?a=b"'>
點此會重載</a>,然後 URL 會發生變化。 - 執行結果:
點此會重載,然後 URL 會發生變化。