欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2024-02-19 15:35:34
...

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;  
}