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

  • Post author:
  • Post category:其他


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

function getParameterByName(name)  
{  
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");  
  var regexS = "[\\?&]"   name   "=([^&#]*)";  
  var regex = new RegExp(regexS);  
  var results = regex.exec(window.location.search);  
  if(results == null)  
    return "";  
  else  
    return decodeURIComponent(results[1].replace(/\ /g, " "));  
}  

function getParameterByName(name) {  
    var match = RegExp('[?&]'   name   '=([^&]*)')  
                    .exec(window.location.search);  
    return match && decodeURIComponent(match[1].replace(/\ /g, ' '));  
} 

var qs = (function(a) {  
    if (a == "") return {};  
    var b = {};  
    for (var i = 0; i < a.length;   i)  
    {  
        var p=a[i].split('=');  
        if (p.length != 2) continue;  
        b[p[0]] = decodeURIComponent(p[1].replace(/\ /g, " "));  
    }  
    return b;  
})(window.location.search.substr(1).split('&'));  

$.urlParam = function(name){  
    var results = new RegExp('[\\?&]'   name   '=([^&#]*)').exec(window.location.href);  
    if (!results)  
    {   
        return 0;   
    }  
    return results[1] || 0;  
}