且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用jQuery/Javascript获取查询字符串参数url值(querystring)

更新时间:2021-07-31 23:06:45

在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

随便使用.