js获取query 参数

var url="http://www.runoob.com/index.php?id=1&image=awesome.jpg";
function getQueryVariable(variable){
   var str=url.split('?');
   var query=str[1];
   var vars=query.split('&');
   console.log(query,str,vars);
   for (var i=0;i<vars.length;i++) {
       var pair = vars[i].split("=");
       console.log(pair);
       if(pair[0] == variable){
           return pair[1];
       }
   }
   return(false);
}
console.log(getQueryVariable('id'));
console.log(getQueryVariable("image"));

你可能感兴趣的:(js)