jQuery插件Query URL Parser用于解析URLs字符串。通過它我們可以方便地獲取協(xié)議、主機(jī)、端口、查詢參數(shù)、文件名、路徑等等。在一些靜態(tài)頁面需要根據(jù)參數(shù)來調(diào)整一些內(nèi)容的時(shí)候這個(gè)插件還是挺有用的。
官方下載(托管在github):http://github.com/allmarkedup/jQuery-URL-Parser
本地下載地址:jQuery-URL-Parser
插件可以返回的數(shù)據(jù)有下面幾項(xiàng):
1 、來源 – URL本身
2 、協(xié)議 – 例如 HTTP,HTTPS,文件等
3 、主機(jī) – 如 blog.,localhost 等
4 、端口 – 例如 80
5 、查詢 – 如果它存在的話是整個(gè)查詢字符串,例如item=value&item2=value2
6 、單個(gè)查詢字符串參數(shù)值
7 、文件 – 該文件名,例如 index.html的
8 、錨 – 哈希(錨)值
9 、路徑 – 文件的路徑(如/folder/dir/index.html)
10 、相對路徑- 包括查詢字符串的相對路徑(如/folder/dir/index.html?item=value)
11 、目錄 – 目錄路徑(如/folder/dir/)
12 、路徑的個(gè)別部分
如果需要獲取上面的 1、2、3、4、7、8、10、11 項(xiàng)的值可以通過使用 .attr() 方法來獲取。
6項(xiàng)可以使用 .param() 方法。
12項(xiàng)可以使用 .segment() 方法。
使用DEMO:
1,使用當(dāng)前頁面的URL(假如地址是http://blog./information/about/index.html?itemID=2&user=dave)
01 | // get the protocol |
02 | jQuery.url.attr( "protocol" ) // returns 'http' |
03 |
04 | // get the path |
05 | jQuery.url.attr( "path" ) // returns '/information/about/index.html' |
06 |
07 | // get the host |
08 | jQuery.url.attr( "host" ) // returns 'blog.' |
09 |
10 | // get the value for the itemID query parameter |
11 | jQuery.url.param( "itemID" ) // returns 2 |
12 |
13 | // get the second segment from the url path |
14 | jQuery.url.segment(2) // returns 'about' |
2,使用其他指定的URL
1 | // set a different URL and return the anchor string |
2 | jQuery.url.setUrl( "http://blog./category/javascript/#footer" ).attr( "anchor" ) // returns 'footer' |