2009年1月11日

location 特徵

特徵 | 方法
location 的特徵

location 是 window 物件下的特徵,你可以用 window.location;也可以省略 window,直接用 location。

hash

URL 中 # 之後的文字,包括 #。# 是用在 的符號。

  • 程式用法:

    <script type='text/javascript'>
    document.write( window.location.hash);
    </script>

  • 執行結果:

host

主機名稱與港號。

hostname

主機名稱。

port

港號。

  • 程式用法:

    <script type='text/javascript'>
    document.write( '主機:'+ location.host 
         +'<br />主機名稱:'+ location.hostname
             +'<br />港號:'+ location.port);
    </script>

  • 執行結果:

    主機:ant4js.blogspot.com
    主機名稱:ant4js.blogspot.com
    港號:

href

完整的 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

    射擊遊戲

pathname

相對於主機的檔案路徑名。

protocol

通信規則。例如 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 會發生變化。


©2008-2009 by ant2legs, All Rights Reserved. ant2legs 擁有其製作的文章,圖片與程式的著作權,所有權利保留。