用 JavaScript 取得目前網址與其它網址參數
首先透過 JavaScript 取得目前網頁網址的語法是 window.location.href,以下簡單範例:
假設我們的網址是:http://www.bigwell.com.tw:80/test.html?id=222#333
// 輸出値為 http://www.bigwell.com.tw/test.html?tid=222#333
window.location.href
window.location.href
// 輸出値為 http:
window.location.protocol
window.location.protocol
// 輸出値為 www.bigwell.com.tw
window.location.hostname
window.location.hostname
// 輸出値為 www.bigwell.com.tw
window.location.host
window.location.host
// 輸出値為 80
window.location.port
window.location.port
// 輸出値為 /test.html
window.location.pathname
window.location.pathname
// 輸出値為 ?tid=222
window.location.search
window.location.search
// 輸出値為 #333
window.location.hash
其他詳細可參考: http://www.w3schools.com/js/js_window_location.asp
沒有留言:
張貼留言