2017年3月15日 星期三

[JavaScript] 用 JavaScript 取得目前網址與其它網址參數

用 JavaScript 取得目前網址與其它網址參數

在 PHP 中我們常常會需要取得網址參數,但並非每次都採用 PHP 來取得網址參數,因為有的時候某些動作僅需要在用戶端執行即可。
首先透過 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
// 輸出値為 http:
window.location.protocol
// 輸出値為 www.bigwell.com.tw
window.location.hostname
// 輸出値為 www.bigwell.com.tw
window.location.host
// 輸出値為 80
window.location.port
// 輸出値為 /test.html
window.location.pathname
// 輸出値為 ?tid=222
window.location.search
// 輸出値為 #333
window.location.hash
其他詳細可參考: http://www.w3schools.com/js/js_window_location.asp

沒有留言:

張貼留言