先以“http://www.cnblogs.com/wuxibolgs329/p/6188619.html#flag?test=12345”為例,然后獲得它的各個組成部分。
1、獲取頁面完整的url
var a=location.href;console.log(a); // “http://www.cnblogs.com/wuxibolgs329/p/5261577.html#flag?test=12345”
2、獲取頁面的域名
var host = window.location.host; //www.cnblogs.comvar host2 = document.domain; //www.cnblogs.comvar a = location.hostname; //www.cnblogs.com
3、獲取url協(xié)議
var a=location.protocol;console.log(a); //http:
4、獲取端口
var a=location.port;console.log(a);
5、獲取頁面路徑
var a=location.pathname;console.log(a);
6、設(shè)置或獲取 URL 的協(xié)議部分
	var a = location.protocol;
7、獲取#后的部分
var a=window.location.hash; var b=a.substr(1); console.log(b); // flag?test=12345
8、獲取 href 屬性中跟在問號?后面的部分
// 此時案例地址變?yōu)?ldquo;http://www.cnblogs.com/wuxibolgs329/p/5261577.html?test=12345”。得到 test=12345var a=location.search;var b=a.substr(1);console.log(b); //如果案例依舊是“http://www.cnblogs.com/wuxibolgs329/p/5261577.html#flag?test=12345”,則需下面的寫法,得到 test=12345var a=location.href;var b=a.substr(a.lastIndexOf('?')+1);console.log(b);9、獲取 = 號后面的部分
var a=location.href;var b=a.substring(a.lastIndexOf('=')+1);console.log(b); // 12345以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持VeVb武林網(wǎng)!
新聞熱點
疑難解答