我见过很多jQuery示例,其中参数大小和名称都是未知的。
我的URL只会有一个字符串
http://example.com?sent=yes
我只想检测:
sent存在吗? 它等于"是"吗?
我见过很多jQuery示例,其中参数大小和名称都是未知的。
我的URL只会有一个字符串
http://example.com?sent=yes
我只想检测:
sent存在吗? 它等于"是"吗?
当前回答
或者你可以用这个简洁的小函数,因为为什么解这么复杂?
function getQueryParam(param, defaultValue = undefined) {
location.search.substr(1)
.split("&")
.some(function(item) { // returns first occurence and stops
return item.split("=")[0] == param && (defaultValue = item.split("=")[1], true)
})
return defaultValue
}
当简化和联机时看起来更好:
单线解
var queryDict = {};
location.search.substr(1).split("&").forEach(function(item) {queryDict[item.split("=")[0]] = item.split("=")[1]})
result: queryDict['sent'] // undefined or 'value'
但如果你有编码字符或多值键呢?
你最好看看这个答案:如何在JavaScript中获得查询字符串值?
偷偷高峰
"?a=1&b=2&c=3&d&e&a=5&a=t%20e%20x%20t&e=http%3A%2F%2Fw3schools.com%2Fmy%20test.asp%3Fname%3Dståle%26car%3Dsaab"
> queryDict
a: ["1", "5", "t e x t"]
b: ["2"]
c: ["3"]
d: [undefined]
e: [undefined, "http://w3schools.com/my test.asp?name=ståle&car=saab"]
> queryDict["a"][1] // "5"
> queryDict.a[1] // "5"
其他回答
如果你想从一个特定的url中找到一个特定的参数:
function findParam(url, param){
var check = "" + param;
if(url.search(check )>=0){
return url.substring(url.search(check )).split('&')[0].split('=')[1];
}
}
var url = "http://www.yourdomain.com/example?id=1&order_no=114&invoice_no=254";
alert(findParam(url,"order_no"));
这是基于Gazoris的答案,但URL解码了参数,因此当它们包含除数字和字母以外的数据时可以使用:
function urlParam(name){
var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
// Need to decode the URL parameters, including putting in a fix for the plus sign
// https://stackoverflow.com/a/24417399
return results ? decodeURIComponent(results[1].replace(/\+/g, '%20')) : null;
}
还有一个使用URI.js库的例子。
例子准确地回答了所问的问题。
var url = 'http://example.com?sent=yes'; var urlParams = new URI(url).search(true); // 1. Does sent exist? var sendExists = urlParams.sent !== undefined; // 2. Is it equal to "yes"? var sendIsEqualtToYes = urlParams.sent == 'yes'; // output results in readable form // not required for production if (sendExists) { console.log('Url has "sent" param, its value is "' + urlParams.sent + '"'); if (urlParams.sent == 'yes') { console.log('"Sent" param is equal to "yes"'); } else { console.log('"Sent" param is not equal to "yes"'); } } else { console.log('Url hasn\'t "sent" param'); } <script src="https://cdnjs.cloudflare.com/ajax/libs/URI.js/1.18.2/URI.min.js"></script>
我总是把它写成一行。params有变量:
params={};location.search.replace(/[?&]+([^=&]+)=([^&]*)/gi,function(s,k,v){params[k]=v})
多行:
var params={};
window.location.search
.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(str,key,value) {
params[key] = value;
}
);
作为一个函数
function getSearchParams(k){
var p={};
location.search.replace(/[?&]+([^=&]+)=([^&]*)/gi,function(s,k,v){p[k]=v})
return k?p[k]:p;
}
你可以这样用:
getSearchParams() //returns {key1:val1, key2:val2}
or
getSearchParams("key1") //returns val1
函数GetRequestParam(参数) { Var res = null; 尝试{ var qs = decodeURIComponent(window.location.search.substring(1));//获取then之后的所有内容'?URI中的' Var ar = q .split('&'); 美元。Each (ar, function(a, b){ Var kv = b.split('='); If (param === kv[0]){ Res = kv[1]; 返回false;//打破循环 } }); }捕捉(e) {} 返回res; }