色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

js獲取url參數值的幾種方式詳解

瀏覽:51日期:2022-06-13 14:57:16
目錄方法一:調用方法方法二:調用方法補充URL知識方法一:

采用正則表達式獲取地址欄參數 (代碼簡潔,重點正則)

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; }調用方法

let 參數1 = GetQueryString("參數名1"));

方法二:

split拆分法 (代碼較復雜,較易理解)

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;}調用方法

let Request = new Object();Request = GetRequest();var 參數1,參數2 ...;參數1 = Request['參數1'];參數2 = Request['參數2'];參數... = Request['參數...'];方法三:split拆分法(易于理解,代碼中規)

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);}

調用方法

let 參數1 = getQueryVariable("參數名1");

補充URL知識

示例url =https://www.jb51.net/list/list_3_1.htm

1、window.location.href(設置或獲取整個 URL 為字符串)console.log(window.location.href)

打印結果:http://www.jianshu.com/search?q=123&page=1&type=note

2、window.location.protocol(設置或獲取 URL 的協議部分)console.log(window.location.protocol)

打印結果:http:

3、window.location.host(設置或獲取 URL 的主機部分)console.log(window.location.host)

打印結果:www.jianshu.com

4、window.location.port(設置或獲取與 URL 關聯的端口號碼)console.log(window.location.port)

打印結果:空字符(如果采用默認的80端口(update:即使添加了:80),那么返回值并不是默認的80而是空字符)

5、window.location.pathname(設置或獲取與 URL 的路徑部分(就是文件地址))console.log(window.location.pathname)

打印結果:/search

6、window.location.search(設置或獲取 href 屬性中跟在問號后面的部分)console.log(window.location.search)

打印結果:?q=123&page=1&type=note

PS:獲得查詢(參數)部分,除了給動態語言賦值以外,我們同樣可以給靜態頁面,并使用javascript來獲得相信應的參數值。

7、window.location.hash(設置或獲取 href 屬性中在井號“#”后面的分段)console.log(window.location.hash)

打印結果:空字符(因為url中沒有)

以上就是js獲取url參數值的幾種方式詳解的詳細內容,更多關于js獲取url參數值的資料請關注好吧啦網其它相關文章!

標簽: JavaScript
主站蜘蛛池模板: 久久久久久九九 | 成年日韩片av在线网站 | 国产欧美日韩精品a在线观看 | 在线看日韩 | 成人免费影视网站 | 国产成人综合精品 | 久草资源网 | 中国a级毛片免费 | 欧美日韩一级黄色片 | 久久99亚洲精品久久久久 | 一区二区三区视频 | 欧美高清另类自拍视频在线看 | 久久免费精品国产72精品剧情 | 99精品久久久久久久免费看蜜月 | 成人欧美一区二区三区在线 | 在线不卡一区二区三区日韩 | 久久久综合结合狠狠狠97色 | 在线免费观看亚洲视频 | 伊人2222| 久久国产精品1区2区3区网页 | 国产成人爱片免费观看视频 | japanesevideo国产在线 | 久久久影院 | 成人性毛片 | 日韩在线视频网址 | 在线观看免费国产成人软件 | 亚洲高清中文字幕一区二区三区 | 毛片搜索 | 国内国语一级毛片在线视频 | 亚洲悠悠色综合中文字幕 | 中文字幕有码在线视频 | 国产高清区 | 亚洲精品一二三 | 亚洲一区二区在线成人 | 深夜一级毛片 | 欧美精品一区二区三区视频 | 国产成人综合亚洲 | 99je全部都是精品视频在线 | 最近手机高清中文字幕大全7 | 91久久国产成人免费观看资源 | 性欧美videos高清精品 |