将字符串拆分为参数,例如 JavaScript 中的 URL
Splitting a string into parameters like a URL in JavaScript
我通常使用此代码从 URL:
获取参数
var QueryString = function () {
// This function is anonymous, is executed immediately and
// the return value is assigned to QueryString!
var query_string = {};
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
// If first entry with this name
if (typeof query_string[pair[0]] === "undefined") {
query_string[pair[0]] = decodeURIComponent(pair[1]);
// If second entry with this name
} else if (typeof query_string[pair[0]] === "string") {
var arr = [ query_string[pair[0]],decodeURIComponent(pair[1]) ];
query_string[pair[0]] = arr;
// If third or later entry with this name
} else {
query_string[pair[0]].push(decodeURIComponent(pair[1]));
}
}
return query_string;
}();
但是,我想传递一个字符串而不是使用 window.location
。但是 search.substring
对此无效。
字符串就像一个 url: a=this&b=that
有什么指点吗?
var QueryString = function (query) {
// This function is anonymous, is executed immediately and
// the return value is assigned to QueryString!
var query_string = {};
//var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
// If first entry with this name
if (typeof query_string[pair[0]] === "undefined") {
query_string[pair[0]] = decodeURIComponent(pair[1]);
// If second entry with this name
} else if (typeof query_string[pair[0]] === "string") {
var arr = [ query_string[pair[0]],decodeURIComponent(pair[1]) ];
query_string[pair[0]] = arr;
// If third or later entry with this name
} else {
query_string[pair[0]].push(decodeURIComponent(pair[1]));
}
}
return query_string;
};
//Sample Call
QueryString('a=this&b=that');
将query
作为参数传递给函数定义并在函数内部注释var query= ...
声明,并删除自执行的()
子句;
现在可以随时随地拨打电话QueryString('a=this&b=that')
我通常使用此代码从 URL:
获取参数var QueryString = function () {
// This function is anonymous, is executed immediately and
// the return value is assigned to QueryString!
var query_string = {};
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
// If first entry with this name
if (typeof query_string[pair[0]] === "undefined") {
query_string[pair[0]] = decodeURIComponent(pair[1]);
// If second entry with this name
} else if (typeof query_string[pair[0]] === "string") {
var arr = [ query_string[pair[0]],decodeURIComponent(pair[1]) ];
query_string[pair[0]] = arr;
// If third or later entry with this name
} else {
query_string[pair[0]].push(decodeURIComponent(pair[1]));
}
}
return query_string;
}();
但是,我想传递一个字符串而不是使用 window.location
。但是 search.substring
对此无效。
字符串就像一个 url: a=this&b=that
有什么指点吗?
var QueryString = function (query) {
// This function is anonymous, is executed immediately and
// the return value is assigned to QueryString!
var query_string = {};
//var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
// If first entry with this name
if (typeof query_string[pair[0]] === "undefined") {
query_string[pair[0]] = decodeURIComponent(pair[1]);
// If second entry with this name
} else if (typeof query_string[pair[0]] === "string") {
var arr = [ query_string[pair[0]],decodeURIComponent(pair[1]) ];
query_string[pair[0]] = arr;
// If third or later entry with this name
} else {
query_string[pair[0]].push(decodeURIComponent(pair[1]));
}
}
return query_string;
};
//Sample Call
QueryString('a=this&b=that');
将query
作为参数传递给函数定义并在函数内部注释var query= ...
声明,并删除自执行的()
子句;
现在可以随时随地拨打电话QueryString('a=this&b=that')