1. 程式人生 > >JavaScript獲取位址列內容

JavaScript獲取位址列內容

例如地址為:http://www.mazey.net/baby/blog/index.php?a=1&b=2#c
var query = window.location.href;	//http://www.mazey.net/baby/blog/index.php?a=1&b=2#c
var query = window.location.host;	//www.mazey.net

1、window.location.href
說明:整個位址列字串(在瀏覽器中就是完整的位址列)
本例返回值: http://www.mazey.net/baby/blog/index.php?a=1&b=2#c

2、window.location.protocol


說明:位址列的協議部分
本例返回值:http:

3、window.location.host
說明:位址列的主機部分
本例返回值:www.mazey.net

4、window.location.port
說明:位址列的埠部分,實際上返回空字元
本例返回值:""

5、window.location.pathname
說明:位址列的路徑部分(就是檔案地址)
本例返回值:/baby/blog/index.php

6、window.location.search
說明:查詢(引數?&)部分
本例返回值:?a=1&b=2

7、window.location.hash
說明:錨點部分(#)
本例返回值:#c