通過js獲取當前頁面url的資訊
在web前端製作中,可以通過javascript獲取當前網頁url的相關資訊,整理如下:
1、設定或獲取物件指定的檔名或路徑
window.location.pathname
2、設定或獲取整個URL為字串
window.location.href
3、設定或獲取與URL關聯的埠號
window.location.port
4、設定或獲取URL的協議部分
window.location.protocol
5、設定或獲取href屬性中在井號“#”後面的分段
window.location.hash
6、設定或獲取location或URL的hostname和port號碼
window.location.host
7、設定或獲取href屬性中跟在問號後面的部分
window.location.search
8、獲取變數的值(擷取等號後面的部分)
var url = window.location.search; alert(url.length); alert(url.lastIndexOf('=')); var loc = url.substring(url.lastIndexOf('=')+1, url.length);
9、獲取當前網頁的域名
document.domain