js获取url的参数和值的N种有效方法

来源:互联网 发布:腾讯网络电影合作地址 编辑:程序博客网 时间:2024/05/22 11:43
转自 http://qiaolevip.iteye.com/blog/1672330
Js代码  收藏代码
  1. function getParameterByName(name)  
  2. {  
  3.   name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");  
  4.   var regexS = "[\\?&]" + name + "=([^&#]*)";  
  5.   var regex = new RegExp(regexS);  
  6.   var results = regex.exec(window.location.search);  
  7.   if(results == null)  
  8.     return "";  
  9.   else  
  10.     return decodeURIComponent(results[1].replace(/\+/g, " "));  
  11. }  


Js代码  收藏代码
  1. var urlParams = {};  
  2. (function () {  
  3.     var match,  
  4.         pl     = /\+/g,  // Regex for replacing addition symbol with a space  
  5.         search = /([^&=]+)=?([^&]*)/g,  
  6.         decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },  
  7.         query  = window.location.search.substring(1);  
  8.   
  9.     while (match = search.exec(query))  
  10.        urlParams[decode(match[1])] = decode(match[2]);  
  11. })();  


Js代码  收藏代码
  1. function getParameterByName(name) {  
  2.     var match = RegExp('[?&]' + name + '=([^&]*)')  
  3.                     .exec(window.location.search);  
  4.     return match && decodeURIComponent(match[1].replace(/\+/g, ' '));  
  5. }  


Js代码  收藏代码
  1. var qs = (function(a) {  
  2.     if (a == ""return {};  
  3.     var b = {};  
  4.     for (var i = 0; i < a.length; ++i)  
  5.     {  
  6.         var p=a[i].split('=');  
  7.         if (p.length != 2) continue;  
  8.         b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));  
  9.     }  
  10.     return b;  
  11. })(window.location.search.substr(1).split('&'));  


Google method 
Js代码  收藏代码
  1. function (b) {  
  2.     var c = typeof b === "undefined";  
  3.     if (a !== h && c) return a;  
  4.     for (var d = {}, b = b || k[B][vb], e = b[p]("?"), f = b[p]("#"), b = (f === -1 ? b[Ya](e + 1) : [b[Ya](e + 1, f - e - 1), "&", b[Ya](f + 1)][K](""))[z]("&"), e = i.dd ? ia : unescape, f = 0, g = b[w]; f < g; ++f) {  
  5.         var l = b[f][p]("=");  
  6.         if (l !== -1) {  
  7.             var q = b[f][I](0, l),  
  8.                 l = b[f][I](l + 1),  
  9.                 l = l[Ca](/\+/g, " ");  
  10.             try {  
  11.                 d[q] = e(l)  
  12.             } catch (A) {}  
  13.         }  
  14.     }  
  15.     c && (a = d);  
  16.     return d  
  17. }  


Js代码  收藏代码
  1. (function($) {  
  2.     $.QueryString = (function(a) {  
  3.         if (a == ""return {};  
  4.         var b = {};  
  5.         for (var i = 0; i < a.length; ++i)  
  6.         {  
  7.             var p=a[i].split('=');  
  8.             if (p.length != 2) continue;  
  9.             b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));  
  10.         }  
  11.         return b;  
  12.     })(window.location.search.substr(1).split('&'))  
  13. })(jQuery);  


Js代码  收藏代码
  1. $.urlParam = function(name){  
  2.     var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);  
  3.     if (!results)  
  4.     {   
  5.         return 0;   
  6.     }  
  7.     return results[1] || 0;  
  8. }  


Js代码  收藏代码
  1. var getParamValue = (function() {  
  2.     var params,  
  3.         resetParams = function() {  
  4.             var query = window.location.search,  
  5.                 regex = /[?&;](.+?)=([^&;]+)/g,  
  6.                 match;  
  7.   
  8.             params = {};  
  9.   
  10.             if (query) {  
  11.                 while (match = regex.exec(query)) {  
  12.                     params[match[1]] = decodeURIComponent(match[2]);  
  13.                 }  
  14.             }      
  15.         };  
  16.   
  17.     window.addEventListener  
  18.     && window.addEventListener('popstate'function() {  
  19.         resetParams();  
  20.     });  
  21.   
  22.     resetParams();  
  23.   
  24.     return function(param) {  
  25.         return params.hasOwnProperty(param) ? params[param] : null;  
  26.     }  
  27.   
  28. })();​  


// The plugin jQuery-URL-Parser https://github.com/allmarkedup/jQuery-URL-Parser 
Js代码  收藏代码
  1. $.url.param("itemID")  


Js代码  收藏代码
  1. ;(function ($) {  
  2.     $.extend({        
  3.         getQueryString: function (name) {             
  4.             function parseParams() {  
  5.                 var params = {},  
  6.                     e,  
  7.                     a = /\+/g,  // Regex for replacing addition symbol with a space  
  8.                     r = /([^&=]+)=?([^&]*)/g,  
  9.                     d = function (s) { return decodeURIComponent(s.replace(a, " ")); },  
  10.                     q = window.location.search.substring(1);  
  11.   
  12.                 while (e = r.exec(q))  
  13.                     params[d(e[1])] = d(e[2]);  
  14.   
  15.                 return params;  
  16.             }  
  17.   
  18.             if (!this.queryStringParams)  
  19.                 this.queryStringParams = parseParams();   
  20.   
  21.             return this.queryStringParams[name];  
  22.         }  
  23.     });  
  24. })(jQuery);  
  25.   
  26. // 使用  
  27. var someVar = $.getQueryString('myParam');  


http://medialize.github.com/URI.js/ 
Js代码  收藏代码
  1. var data = URI('?foo=bar&bar=baz&foo=world').query(true);  
  2. data == {  
  3.   "foo": ["bar""world"],  
  4.   "bar""baz"  
  5. }  


Js代码  收藏代码
  1. function getUrlParams() {  
  2.     var result = {};  
  3.     var params = (window.location.search.split('?')[1] || '').split('&');  
  4.     for(var param in params) {  
  5.         if (params.hasOwnProperty(param)) {  
  6.             paramParts = params[param].split('=');  
  7.             result[paramParts[0]] = decodeURIComponent(paramParts[1] || "");  
  8.         }  
  9.     }  
  10.     return result;  
  11. }  
0 0
原创粉丝点击