javascript擷取網頁URL地址及參數等

來源:互聯網
上載者:User

先看一個樣本

用javascript擷取url網址資訊

Java代碼

<script type="text/javascript">   document.write("location.host="+location.host+"<br>");   document.write("location.hostname="+location.hostname+"<br>");   document.write("location.href="+location.href+"<br>");   document.write("location.pathname="+location.pathname+"<br>");   document.write("location.protocol="+location.protocol+"<br>");   </script>  <script type="text/javascript">document.write("location.host="+location.host+"<br>");document.write("location.hostname="+location.hostname+"<br>");document.write("location.href="+location.href+"<br>");document.write("location.pathname="+location.pathname+"<br>");document.write("location.protocol="+location.protocol+"<br>");</script>

執行 用javascript擷取url網址資訊 代碼效果如下

Java代碼

location.host=www.vnde.cn   location.hostname=www.vnde.cn   location.href=http://www.vnde.cn/bc/2008/0306/article_1860.html   location.pathname=/bc/2008/0306/article_1860.html   location.protocol=http:  location.host=www.vnde.cnlocation.hostname=www.vnde.cnlocation.href=http://www.vnde.cn/bc/2008/0306/article_1860.htmllocation.pathname=/bc/2008/0306/article_1860.htmllocation.protocol=http:

詳細介紹 window.location方法擷取URL
統一資源定位器 (Uniform Resource Locator, URL) 完整的URL由這幾個部分構成:
scheme://host:port/path?query#fragment

scheme:通訊協定
常用的http,ftp,maito等

host:主機
伺服器(電腦)網域名稱系統 (DNS) 主機名稱或 IP 位址。

port:連接埠號碼
整數,可選,省略時使用方式情節的預設連接埠,如http的預設連接埠為80。

path:路徑
由零或多個”/”符號隔開的字串,一般用來表示主機上的一個目錄或檔案地址。

query:查詢
可選,用於給動態網頁(如使用CGI、ISAPI、PHP/JSP/ASP/ASP.NET等技術製作的網頁)傳遞參數,可有多個參數,用”&”符號隔開,每個參數的名和值用”=”符號隔開。

fragment:資訊片斷
字串,用於指定網路資源中的片斷。例如一個網頁中有多個名詞解釋,可使用fragment直接定位到某一名詞解釋。(也稱為錨點.)

樣本:

1, window.location.href
整個URl字串(在瀏覽器中就是完整的地址欄)
傳回值:http://www.2astudio.com:80/view.asp?id=209#cmt1323

2,window.location.protocol
URL 的協議部分
傳回值:http:

3,window.location.host
URL 的主機部分,
傳回值:www.2astudio.com

4,window.location.port
URL 的連接埠部分。如果採用預設的80連接埠(update:即使添加了:80),那麼傳回值並不是預設的80而是Null 字元。
本例傳回值:空

5,window.location.pathname
URL 的路徑部分(就是檔案地址)
傳回值:/view.asp

6,window.location.search
查詢(參數)部分。除了給動態語言賦值以外,我們同樣可以給靜態頁面,並使用javascript來獲得相信應的參數值
傳回值:?id=209

7,window.location.hash
錨點
傳回值:#cmt1323

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.