Sin*_*hus 301 javascript url url-parameters query-string
我正在寻找一个可以获取URL参数的jQuery插件,并支持此搜索字符串而不输出JavaScript错误:"格式错误的URI序列".如果没有支持这个的jQuery插件,我需要知道如何修改它来支持这个.
?search=%E6%F8%E5
Run Code Online (Sandbox Code Playgroud)
解码后,URL参数的值应为:
æøå
Run Code Online (Sandbox Code Playgroud)
(字符是挪威语).
我无权访问服务器,因此我无法对其进行任何修改.
Jam*_*mes 417
function getURLParameter(name) {
return decodeURI(
(RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
);
}
Run Code Online (Sandbox Code Playgroud)
rad*_*and 293
以下是我从这里的评论中创建的内容,以及修复未提及的错误(例如实际返回null,而不是'null'):
function getURLParameter(name) {
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null;
}
Run Code Online (Sandbox Code Playgroud)
Luc*_*cas 107
你真正想要的是jQuery URL Parser插件.使用此插件,获取特定URL参数(对于当前URL)的值如下所示:
$.url().param('foo');
Run Code Online (Sandbox Code Playgroud)
如果你想要一个带参数名称的对象作为键和参数值作为值,你只需要不param()
带参数调用,如下所示:
$.url().param();
Run Code Online (Sandbox Code Playgroud)
此库也适用于其他网址,而不仅仅是当前网址:
$.url('http://allmarkedup.com?sky=blue&grass=green').param();
$('#myElement').url().param(); // works with elements that have 'src', 'href' or 'action' attributes
Run Code Online (Sandbox Code Playgroud)
由于这是一个完整的URL解析库,您还可以从URL获取其他信息,如指定的端口,或路径,协议等:
var url = $.url('http://allmarkedup.com/folder/dir/index.html?item=value');
url.attr('protocol'); // returns 'http'
url.attr('path'); // returns '/folder/dir/index.html'
Run Code Online (Sandbox Code Playgroud)
它还有其他功能,请查看其主页以获取更多文档和示例.
而不是写你自己的URI解析器为这一特定目的的还挺作品在大多数情况下,使用一个实际的URI解析器.根据答案,来自其他答案的代码可以返回'null'
而不是null
,不能使用空参数(?foo=&bar=x
),不能一次解析并返回所有参数,如果您反复查询参数的URL等,则重复工作.
使用实际的URI解析器,不要发明自己的.
对于那些厌恶jQuery的人来说,有一个纯JS的插件版本.
Edw*_*son 43
如果您不知道URL参数是什么,并希望获得具有参数中的键和值的对象,则可以使用以下命令:
function getParameters() {
var searchString = window.location.search.substring(1),
params = searchString.split("&"),
hash = {};
if (searchString == "") return {};
for (var i = 0; i < params.length; i++) {
var val = params[i].split("=");
hash[unescape(val[0])] = unescape(val[1]);
}
return hash;
}
Run Code Online (Sandbox Code Playgroud)
使用类似url调用getParameters()/posts?date=9/10/11&author=nilbus
将返回:
{
date: '9/10/11',
author: 'nilbus'
}
Run Code Online (Sandbox Code Playgroud)
我不会在这里包含代码,因为它甚至离问题更远,但weareon.net发布了一个库,它允许操作URL中的参数:
CMS*_*CMS 40
您可以使用浏览器本机location.search属性:
function getParameter(paramName) {
var searchString = window.location.search.substring(1),
i, val, params = searchString.split("&");
for (i=0;i<params.length;i++) {
val = params[i].split("=");
if (val[0] == paramName) {
return unescape(val[1]);
}
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
但是有一些jQuery插件可以帮助你:
Eug*_*ash 25
基于999的答案:
function getURLParameter(name) {
return decodeURIComponent(
(location.search.match(RegExp("[?|&]"+name+'=(.+?)(&|$)'))||[,null])[1]
);
}
Run Code Online (Sandbox Code Playgroud)
变化:
decodeURI()
被替换为 decodeURIComponent()
[?|&]
在正则表达式的开头添加小智 6
需要添加i参数以使其不区分大小写:
function getURLParameter(name) {
return decodeURIComponent(
(RegExp(name + '=' + '(.+?)(&|$)', 'i').exec(location.search) || [, ""])[1]
);
}
Run Code Online (Sandbox Code Playgroud)
你不应该使用 jQuery 来做这样的事情!
\n现代方法是通过像 Bower 这样的包管理器来使用小型可重用模块。
我创建了一个小模块,可以将查询字符串解析为对象。像这样使用它:
\n\n// parse the query string into an object and get the property\nqueryString.parse(unescape(location.search)).search;\n//=> \xc3\xa6\xc3\xb8\xc3\xa5\n
Run Code Online (Sandbox Code Playgroud)\n