获取地址栏参数转为对象

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <h1>获取地址栏参数转一下,模拟一个url</h1>

    <script>
        let url = "http://www.baidu.com?name=liugaoyin&age=22&height=178";

        let obj=turnUrl(url);
        console.log(obj)
        function turnUrl(url){
            let index = url.indexOf("?");
            url = url.substring(index+1);
            let arr = url.split("&");
            let obj = {}
            for(let i=0;i<arr.length;i++){
                let arr1 = arr[i].split("=");
                obj[arr1[0]]=arr1[1];
            }
            return obj;
        }
    </script>
</body>
</html>
posted @ 2019-09-12 15:38  高二going  阅读(259)  评论(0编辑  收藏  举报