编写一个参数JavaScript函数parseQueryString,它的用途是把url参数解析为一个对象

var url = "http://www.taobao.com/index.php?key0=0&key1=1&key2=2.............";
var obj = parseQueryString(url);
       
function parseQueryString(argu){
  var str = argu.split('?')[1];
  var result = {};
  var temp = str.split('&');
  for(var i=0; i<temp.length; i++)
  {
     var temp2 = temp[i].split('=');
     result[temp2[0]] = temp2[1];
  }
  return result;
}
<body>
        <script type="text/javascript">
            function parseQueryString(argu){
              var str = argu.split('?')[1];
              var result = {};
              var temp = str.split('&');
              for(var i=0; i<temp.length; i++)
              {
                 var temp2 = temp[i].split('=');
                 result[temp2[0]] = temp2[1];
              }
              return result;
            }
            var url = "http://www.taobao.com/index.php?key=123456&name=xiangruding&value=20170308&mid=1830246&token=loyyehkvk";
            var obj = parseQueryString(url);
            console.log(obj);
            console.log("mid: "+obj.mid);
            
            //对象转换成字符串
            var arr1=JSON.stringify(obj)
            console.log(arr1);
            
            //将字符串转换成对象 
            var zobj=JSON.parse(arr1)
            console.log(zobj);
        </script>
    </body>

结果:

 

posted @ 2017-03-07 12:32  红妆满面  阅读(4504)  评论(0编辑  收藏  举报