我有一个带有一些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执行此操作?
当前回答
我一次又一次遇到同样的问题。现在这里的许多用户现在我以我的HAX工作而闻名,
所以我用以下方法来解决:
PHP:
echo "<p style="display:none" id=\"hidden-GET\">".$_GET['id']."</p>";
JS:
document.getElementById("hidden-GET").innerHTML;
简单的HAX但有效。
其他回答
我使用
function getVal(str) {
var v = window.location.search.match(new RegExp('(?:[\?\&]'+str+'=)([^&]+)'));
return v ? v[1] : null;
}
获取单个参数值:
function getQueryParameter(query, parameter) {
return (window.location.href.split(parameter + '=')[1].split('&')[0]);}
ECMAScript 6解决方案:
var params = window.location.search
.substring(1)
.split("&")
.map(v => v.split("="))
.reduce((map, [key, value]) => map.set(key, decodeURIComponent(value)), new Map())
还有一个建议。
已经有一些很好的答案,但我发现它们不必要地复杂,难以理解。这是一个简短、简单的数组,它返回一个简单的关联数组,其中键名与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”。
下面是一个获取单个参数的简单函数:
function getUrlParam(paramName) {
var match = window.location.search.match("[?&]" + paramName + "(?:&|$|=([^&]*))");
return match ? (match[1] ? decodeURIComponent(match[1]) : "") : null;
}
这些特殊情况的处理与URLSearchParams一致:
如果缺少参数,则返回null。如果参数存在但没有“=”(例如“?param”),则返回“”。
笔记如果参数名称可能包含特殊的URL或正则字符(例如,如果它来自用户输入),则需要对其进行转义
function getUrlParamWithSpecialName(paramName) {
return getUrlParam(encodeURIComponent(paramName).replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
}