使用jQuery/Javascript获取查询字符串参数url值(querystring) [英] Get query string parameters url values with jQuery / Javascript (querystring)
问题描述
任何人都知道编写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();
在以下URL中,谁会给我值"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.
推荐答案
在SitePoint上从我们的朋友那里找到了这颗宝石. https://www.sitepoint.com/url-parameters-jquery/.
Found this gem from our friends over at SitePoint. https://www.sitepoint.com/url-parameters-jquery/.
使用纯jQuery.我只是用这个,它起作用了.例如,对它进行了一些调整.
Using PURE jQuery. I just used this and it worked. Tweaked it a bit for example sake.
//URL is http://www.example.com/mypage?ref=registration&email=bobo@example.com
$.urlParam = function (name) {
var results = new RegExp('[\?&]' + name + '=([^&#]*)')
.exec(window.location.search);
return (results !== null) ? results[1] || 0 : false;
}
console.log($.urlParam('ref')); //registration
console.log($.urlParam('email')); //bobo@example.com
随便使用.
这篇关于使用jQuery/Javascript获取查询字符串参数url值(querystring)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!