Javascript 从jQuery中的URL提取搜索查询
可能重复:Javascript 从jQuery中的URL提取搜索查询,javascript,jquery,Javascript,Jquery,可能重复: 如何在jQuery中创建三个独立的变量,其值分别为example、test和more 换句话说,如何根据URL在URL中的位置(第一、第二、第三等)或查询的&other=部分(不确定其名称)从URL提取查询?我使用现有插件执行以下任务:我使用此方法: var query = window.location.split('?')[1]; // or http://www.example.org/search?q=example&another=test&
如何在jQuery中创建三个独立的变量,其值分别为
example
、test
和more
换句话说,如何根据URL在URL中的位置(第一、第二、第三等)或查询的
&other=
部分(不确定其名称)从URL提取查询?我使用现有插件执行以下任务:我使用此方法:
var query = window.location.split('?')[1]; // or http://www.example.org/search?q=example&another=test&again=more
var query_obj = unserialize(query);
function unserialize(query) {
var pair, params = {};
query = query.replace(/^\?/, '').split(/&/);
for (pair in query) {
pair = query[pair].split('=');
params[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
}
return params;
}
因此,您将在query\u obj
中使用如下变量:query\u obj.test
我不久前在互联网上发现了这个功能,所以我无法提供链接。很抱歉,感谢最先发布它的人。不完全确定您想要什么,您能给我们举个输入和输出的例子吗?
var query = window.location.split('?')[1]; // or http://www.example.org/search?q=example&another=test&again=more
var query_obj = unserialize(query);
function unserialize(query) {
var pair, params = {};
query = query.replace(/^\?/, '').split(/&/);
for (pair in query) {
pair = query[pair].split('=');
params[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
}
return params;
}