js獲取url參數(shù)值的幾種方式詳解
方法一:
采用正則表達(dá)式獲取地址欄參數(shù) (代碼簡潔,重點(diǎn)正則)
function getQueryString(name) { let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); let r = window.location.search.substr(1).match(reg); if (r != null) { return unescape(r[2]); }; return null; }
調(diào)用方法
let 參數(shù)1 = GetQueryString("參數(shù)名1"));
方法二:
split拆分法 (代碼較復(fù)雜,較易理解)
function GetRequest() { const url = location.search; //獲取url中"?"符后的字串 let theRequest = new Object(); if (url.indexOf("?") != -1) { let str = url.substr(1); strs = str.split("&"); for(let i = 0; i < strs.length; i ++) { theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]); } } return theRequest; }
調(diào)用方法
let Request = new Object();
Request = GetRequest();
var 參數(shù)1,參數(shù)2 ...;
參數(shù)1 = Request['參數(shù)1'];
參數(shù)2 = Request['參數(shù)2'];
參數(shù)... = Request['參數(shù)...'];
方法三:split拆分法(易于理解,代碼中規(guī))
function getQueryVariable(variable){ let query = window.location.search.substring(1); let vars = query.split("&"); for (let i=0;i<vars.length;i++) { let pair = vars[i].split("="); if(pair[0] == variable){return pair[1];} } return(false); }
調(diào)用方法
let 參數(shù)1 = getQueryVariable("參數(shù)名1");
補(bǔ)充URL知識
1、window.location.href(設(shè)置或獲取整個 URL 為字符串)
console.log(window.location.href)
打印結(jié)果:http://www.jianshu.com/search?q=123&page=1&type=note
2、window.location.protocol(設(shè)置或獲取 URL 的協(xié)議部分)
console.log(window.location.protocol)
打印結(jié)果:http:
3、window.location.host(設(shè)置或獲取 URL 的主機(jī)部分)
console.log(window.location.host)
打印結(jié)果:www.jianshu.com
4、window.location.port(設(shè)置或獲取與 URL 關(guān)聯(lián)的端口號碼)
console.log(window.location.port)
打印結(jié)果:空字符(如果采用默認(rèn)的80端口(update:即使添加了:80),那么返回值并不是默認(rèn)的80而是空字符)
5、window.location.pathname(設(shè)置或獲取與 URL 的路徑部分(就是文件地址))
console.log(window.location.pathname)
打印結(jié)果:/search
6、window.location.search(設(shè)置或獲取 href 屬性中跟在問號后面的部分)
console.log(window.location.search)
打印結(jié)果:?q=123&page=1&type=note
PS:獲得查詢(參數(shù))部分,除了給動態(tài)語言賦值以外,我們同樣可以給靜態(tài)頁面,并使用javascript來獲得相信應(yīng)的參數(shù)值。
7、window.location.hash(設(shè)置或獲取 href 屬性中在井號“#”后面的分段)
console.log(window.location.hash)
打印結(jié)果:空字符(因?yàn)閡rl中沒有)
以上就是js獲取url參數(shù)值的幾種方式詳解的詳細(xì)內(nèi)容,更多關(guān)于js獲取url參數(shù)值的資料請關(guān)注腳本之家其它相關(guān)文章!
- 使用JavaScript解析URL參數(shù)為對象的多種方法
- JavaScript 獲取 URL 中參數(shù)值的方法
- JavaScript 獲取 URL 中參數(shù)值的方法詳解(最新整理)
- JavaScript獲取URL中參數(shù)值的四種方法
- JavaScript獲取當(dāng)前頁面url參數(shù)的方法詳解
- JavaScript實(shí)現(xiàn)獲取URL中參數(shù)值的4種方法
- 使用JavaScript獲取URL參數(shù)的方法總結(jié)
- JavaScript獲取URL參數(shù)的幾種方法小結(jié)
- JavaScript獲取URL參數(shù)常見的4種方法
- JavaScript獲取URL參數(shù)的四種方法總結(jié)
- JS獲取URL網(wǎng)址中參數(shù)的幾種方法小結(jié)
相關(guān)文章
記錄微信小程序 height: calc(xx - xx);無效問題
這篇文章主要介紹了微信小程序 - height: calc(xx - xx);無效 問題,文中給大家擴(kuò)展介紹下jquery點(diǎn)擊添加樣式,再次點(diǎn)擊移除樣式的實(shí)例代碼,需要的朋友可以參考下2019-12-12用js腳本控制asp.net下treeview的NodeCheck的實(shí)現(xiàn)代碼
根據(jù)TreeView2.js修改后的TreeView父節(jié)點(diǎn)與子節(jié)點(diǎn)的CheckBox聯(lián)動.2010-03-03JSON基本語法及與JavaScript的異同實(shí)例分析
這篇文章主要介紹了JSON基本語法及與JavaScript的異同,結(jié)合實(shí)例形式分析了json簡單值、對象、數(shù)組三種類型值使用技巧,需要的朋友可以參考下2019-01-01詳解如何使用JavaScript實(shí)現(xiàn)自定義的雙向數(shù)據(jù)綁定
雙向數(shù)據(jù)綁定是一種編程模式,用于在用戶界面和數(shù)據(jù)模型之間實(shí)現(xiàn)數(shù)據(jù)的同步更新,它允許用戶界面中的數(shù)據(jù)變化自動更新到數(shù)據(jù)模型中,在這篇文章中,我會使用基于觀察者模式和基于Proxy對象來實(shí)現(xiàn)JS的自定義雙向數(shù)據(jù)綁定2023-08-08