JS获取URL的参数值

有时候我们需要获取url的id值作为参数传给后端
例如当前的url为
https://blog.wmxxxx.com/index.php?c=account&a=post&uniacid=1&id=1&account_type=1

函数

function getQueryVariable(variable){
   var query = window.location.search.substring(1);
   var vars = query.split("&");
   for (var i=0;i<vars.length;i++) {
       var pair = vars[i].split("=");
       if(pair[0] == variable){return pair[1];}
   }
   return(false);
}

用法

getQueryVariable(id);

就可以获取到urk中get参数为ID的值

Last modification:July 3rd, 2019 at 12:45 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment

简爱博客