URL:http://127.0.0.1:8080/index.html?name=abc&age=13#public/aaa
1、window.location.href(設置或獲取整個 URL 為字符串)
var test = window.location.href;
alert(test);
返回:http://127.0.0.1:8080/index.html?name=abc&age=13#public/aaa
2、window.location.protocol(設置或獲取 URL 的協(xié)議部分)
var test = window.location.protocol;
alert(test);
返回:http:
3、window.location.host(設置或獲取 URL 的主機部分)
var test = window.location.host;
alert(test);
返回:127.0.0.1:8080
4、window.location.port(設置或獲取與 URL 關聯(lián)的端口號碼)
var test = window.location.port;
alert(test);
返回:8080(默認是80)
5、window.location.pathname(設置或獲取與 URL 的路徑部分(就是文件地址))
var test = window.location.pathname;
alert(test);
返回:/index.html
6、window.location.search(設置或獲取 href 屬性中跟在問號后面的部分)
var test = window.location.search;
alert(test);
返回:?name=abc&age=13
獲得查詢(參數(shù))部分,除了給動態(tài)語言賦值以外,我們同樣可以給靜態(tài)頁面,并使用javascript來獲得相信應的參數(shù)值。
7、window.location.hash(設置或獲取 href 屬性中在井號“#”后面的分段)
var test = window.location.hash;
alert(test);
返回:#public/aaa