詳解javascript獲取url信息的常見方法
先以“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.com var host2 = document.domain; //www.cnblogs.com var 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)椤癶ttp://www.cnblogs.com/wuxibolgs329/p/5261577.html?test=12345”。得到 test=12345 var a=location.search; var b=a.substr(1); console.log(b); //如果案例依舊是“http://www.cnblogs.com/wuxibolgs329/p/5261577.html#flag?test=12345”,則需下面的寫法,得到 test=12345 var 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)容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關(guān)文章
Web componentd組件內(nèi)部事件回調(diào)及痛點剖析
這篇文章主要為大家介紹了Web componentd組件內(nèi)部事件回調(diào)示例及其痛點的剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2021-11-11JavaScript中this的四個綁定規(guī)則總結(jié)
相信大家都知道,ES5及之前時代的JavaScript中this的綁定機制是讓很多開發(fā)者頭疼不已的事情。this 的綁定變化多端,讓筆者也吃了不少虧。所以本文總結(jié)了this的四條綁定規(guī)則,在此記錄,以防自己遺忘,也方便他人參考借鑒。下面來一起看看吧。2016-09-09深入理解javascript嚴格模式(Strict Mode)
Strict mode是JavaScript1.8.5引進的技術(shù),但還沒有瀏覽器確實可靠的實現(xiàn)了嚴格模式,所以使用時要小心并且多測試。Strict mode可以應用于整個腳本,也可以適合于單個函數(shù)。2014-11-11