我有一个带有一些GET参数的URL,如下所示:
www.test.com/t.html?a=1&b=3&c=m2-m3-m4-m5
我需要得到c的全部值。我试图读取URL,但只得到m2。如何使用JavaScript执行此操作?
我有一个带有一些GET参数的URL,如下所示:
www.test.com/t.html?a=1&b=3&c=m2-m3-m4-m5
我需要得到c的全部值。我试图读取URL,但只得到m2。如何使用JavaScript执行此操作?
当前回答
我们可以以更简单的方式获取c参数值,而不必循环所有参数,请参见下面的jQuery获取参数。
1.获取参数值:
var url = "www.test.com/t.html?a=1&b=3&c=m2-m3-m4-m5";
url.match(**/(c=)[0-9A-Za-z-]+/ig**)[0].replace('c=',"")
(or)
url.match(**/(c=)[0-z-]+/ig**)[0].replace('c=',"")
以字符串形式返回
“m2-m3-m4-m5”
2.要替换参数值:
var url = "www.test.com/t.html?a=1&b=3&c=m2-m3-m4-m5";
url.replace(**/(c=)[0-9A-Za-z-]+/ig, "c=m2345"**)
其他回答
我见过的大多数实现都错过了URL对名称和值的解码。
下面是一个通用的实用程序函数,它也可以进行正确的URL解码:
function getQueryParams(qs) {
qs = qs.split('+').join(' ');
var params = {},
tokens,
re = /[?&]?([^=]+)=([^&]*)/g;
while (tokens = re.exec(qs)) {
params[decodeURIComponent(tokens[1])] = decodeURIComponent(tokens[2]);
}
return params;
}
//var query = getQueryParams(document.location.search);
//alert(query.foo);
来源
function gup( name, url ) {
if (!url) url = location.href;
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( url );
return results == null ? null : results[1];
}
gup('q', 'hxxp://example.com/?q=abc')
正如在最新浏览器的第一个答案中提到的,我们可以使用新的URL api,然而,获取对象中的所有参数并使用它们的更一致的本地javascript简单解决方案可能是
例如,该类表示locationUtil
const locationSearch = () => window.location.search;
const getParams = () => {
const usefulSearch = locationSearch().replace('?', '');
const params = {};
usefulSearch.split('&').map(p => {
const searchParam = p.split('=');
const [key, value] = searchParam;
params[key] = value;
return params;
});
return params;
};
export const searchParams = getParams();
用法::现在可以在类中导入searchParams对象
url示例---https://www.google.com?key1=https://www.linkedin.com/in/spiara/&valid=true
import { searchParams } from '../somewhere/locationUtil';
const {key1, valid} = searchParams;
if(valid) {
console.log("Do Something");
window.location.href = key1;
}
简化版,已测试
function get(name){
var r = /[?&]([^=#]+)=([^&#]*)/g,p={},match;
while(match = r.exec(window.location)) p[match[1]] = match[2];
return p[name];
}
用法:
var parameter=获取['parameter']
还有一个建议。
已经有一些很好的答案,但我发现它们不必要地复杂,难以理解。这是一个简短、简单的数组,它返回一个简单的关联数组,其中键名与URL中的令牌名相对应。
我为那些想学习的人添加了一个带有评论的版本。
注意,它的循环依赖于jQuery($.each),我建议使用jQuery而不是forEach。我发现,全面使用jQuery来确保跨浏览器兼容性比插入单独的补丁来支持旧浏览器不支持的新功能更简单。
编辑:在我写了这篇文章后,我注意到埃里克·埃利奥特的回答几乎相同,尽管它使用了forEach,而我通常反对(出于上述原因)。
function getTokens(){
var tokens = [];
var query = location.search;
query = query.slice(1);
query = query.split('&');
$.each(query, function(i,value){
var token = value.split('=');
var key = decodeURIComponent(token[0]);
var data = decodeURIComponent(token[1]);
tokens[key] = data;
});
return tokens;
}
注释版本:
function getTokens(){
var tokens = []; // new array to hold result
var query = location.search; // everything from the '?' onward
query = query.slice(1); // remove the first character, which will be the '?'
query = query.split('&'); // split via each '&', leaving us an array of something=something strings
// iterate through each something=something string
$.each(query, function(i,value){
// split the something=something string via '=', creating an array containing the token name and data
var token = value.split('=');
// assign the first array element (the token name) to the 'key' variable
var key = decodeURIComponent(token[0]);
// assign the second array element (the token data) to the 'data' variable
var data = decodeURIComponent(token[1]);
tokens[key] = data; // add an associative key/data pair to our result array, with key names being the URI token names
});
return tokens; // return the array
}
对于下面的示例,我们将假设此地址:
http://www.example.com/page.htm?id=4&name=murray
您可以将URL令牌分配给自己的变量:
var tokens = getTokens();
然后按名称引用每个URL令牌,如下所示:
document.write( tokens['id'] );
这将打印“4”。
您也可以直接从函数中引用一个令牌名称:
document.write( getTokens()['name'] );
…上面会印着“murray”。