使用jQuery获取查询字符串参数

问题描述:

任何人都知道编写jQuery扩展来处理查询字符串参数的好方法吗?我基本上想扩展jQuery magic ($)函数,这样我就可以这样做:

Anyone know of a good way to write a jQuery extension to handle query string parameters? I basically want to extend the jQuery magic ($) function so I can do something like this:

$('?search').val(); 

这会给我以下网址中的值test: http ://www.example.com/index.php?search = test

Which would give me the value "test" in the following URL: http://www.example.com/index.php?search=test.

我见过很多可以做的功能这在jQuery和Javascript中,但实际上我想扩展jQuery,就像上面显示的那样。我不是在寻找一个jQuery插件,我正在寻找jQuery方法的扩展。

I've seen a lot of functions that can do this in jQuery and Javascript, but I actually want to extend jQuery to work exactly as it is shown above. I'm not looking for a jQuery plugin, I'm looking for an extension to the jQuery method.

JQuery jQuery-URL-Parser 插件执行相同的工作,例如检索搜索查询字符串参数的值,可以使用

JQuery jQuery-URL-Parser plugin do the same job, for example to retrieve the value of search query string param, you can use

$.url().param('search');

此库没有积极维护。根据同一插件的作者的建议,您可以使用 URI.js

This library is not actively maintained. As suggested by the author of the same plugin, you can use URI.js.

或者您可以使用 js-url 相反。它非常类似于下面的那个。

Or you can use js-url instead. Its quite similar to the one below.

所以你可以访问查询参数,如 $。url('?search')

So you can access the query param like $.url('?search')