1. 程式人生 > >獲取當前介面的URl地址各部分

獲取當前介面的URl地址各部分

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

設定或獲取物件指定的檔名或路徑。window.location.pathname例:http://localhost:8086/topic/index?topicId=361alert(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=361alert(window.location.port); 則輸出:8086

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

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

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

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