jquery獲取當前頁面的URL信息

以前在做網站的時候,經常會遇到當前頁的分類高亮顯示,以便讓用戶瞭解當前處於哪個頁面。之前一直是在每個不同頁面寫方法。工程量大,也不便於修改。一直在想有什麼簡便的方法實現。後來在網上查到可以用獲取當前URL的詳細信息來判斷。所以收集了JQUERY獲取URL的函數的資料。

設置或獲取對象指定的文件名或路徑。
window.location.pathname
例:http://localhost:8086/topic/index?topicId=361
alert(window.location.pathname); 則輸出:/topic/index

設置或獲取整個 URL 爲字符串。
window.location.href
例:http://localhost:8086/topic/index?topicId=361
alert(window.location.href); 則輸出:http://localhost:8086/topic/index?topicId=361

設置或獲取與 URL 關聯的端口號碼。
window.location.port
例:http://localhost:8086/topic/index?topicId=361
alert(window.location.port); 則輸出:8086

設置或獲取 URL 的協議部分。
window.location.protocol
例:http://localhost:8086/topic/index?topicId=361
alert(window.location.protocol); 則輸出:http:

設置或獲取 href 屬性中在井號“#”後面的分段
window.location.hash

設置或獲取 location 或 URL 的 hostname 和 port 號碼
window.location.host
例:http://localhost:8086/topic/index?topicId=361
alert(window.location.host); 則輸出:http:localhost:8086

設置或獲取 href 屬性中跟在問號後面的部分。
window.location.search
例:http://localhost:8086/topic/index?topicId=361
alert(window.location.search); 則輸出:?topicId=361

window.location
屬性                  描述
hash                設置或獲取 href 屬性中在井號“#”後面的分段。
host                 設置或獲取 location 或 URL 的 hostname 和 port 號碼。
hostname      設置或獲取 location 或 URL 的主機名稱部分。
href                  設置或獲取整個 URL 爲字符串。
pathname      設置或獲取對象指定的文件名或路徑。
port                  設置或獲取與 URL 關聯的端口號碼。
protocol          設置或獲取 URL 的協議部分。
search            設置或獲取 href 屬性中跟在問號後面的部分。

轉載地址:http://www.cnblogs.com/hongmaju/p/5510988.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章