詳解javascript獲取url信息的常見方法
先以“http://www.cnblogs.com/wuxibolgs329/p/6188619.html#flag?test=12345”為例,然后獲得它的各個(gè)組成部分。
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 屬性中跟在問號(hào)?后面的部分
// 此時(shí)案例地址變?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、獲取 = 號(hào)后面的部分
var a=location.href; var b=a.substring(a.lastIndexOf('=')+1); console.log(b); // 12345
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
Web componentd組件內(nèi)部事件回調(diào)及痛點(diǎn)剖析
這篇文章主要為大家介紹了Web componentd組件內(nèi)部事件回調(diào)示例及其痛點(diǎn)的剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2021-11-11js實(shí)現(xiàn)省市聯(lián)動(dòng)效果的簡(jiǎn)單實(shí)例
本篇文章主要是對(duì)js實(shí)現(xiàn)省市聯(lián)動(dòng)效果的簡(jiǎn)單實(shí)例進(jìn)行了介紹,需要的朋友可以過來,希望對(duì)大家有所幫助2014-02-02JavaScript中this的四個(gè)綁定規(guī)則總結(jié)
相信大家都知道,ES5及之前時(shí)代的JavaScript中this的綁定機(jī)制是讓很多開發(fā)者頭疼不已的事情。this 的綁定變化多端,讓筆者也吃了不少虧。所以本文總結(jié)了this的四條綁定規(guī)則,在此記錄,以防自己遺忘,也方便他人參考借鑒。下面來一起看看吧。2016-09-09js中用cssText設(shè)置css樣式的簡(jiǎn)單方法
下面小編就為大家?guī)硪黄猨s中用cssText設(shè)置css樣式的簡(jiǎn)單方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09深入理解javascript嚴(yán)格模式(Strict Mode)
Strict mode是JavaScript1.8.5引進(jìn)的技術(shù),但還沒有瀏覽器確實(shí)可靠的實(shí)現(xiàn)了嚴(yán)格模式,所以使用時(shí)要小心并且多測(cè)試。Strict mode可以應(yīng)用于整個(gè)腳本,也可以適合于單個(gè)函數(shù)。2014-11-11