获取url路径字符

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Mei12656276/article/details/86599595
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    var str = "https://www.baidu.com/baidu?tn=monline_3_dg&ie=utf-8&wd=bz"

    //获取问号后面所有的字符串
    function getUrl(str) {
        var index = str.indexOf("?");
        console.log(index);
        str = str.substr(index + 1)
        //把&符号分隔掉
       var arr = str.split("&")
        console.log(arr);
        //创建对象遍历数组
        var obj ={};
        for(var i=0;i<arr.length;i++){
            //遍历分割掉
          var newArr  =  arr[i].split("=");
          obj[newArr[0]]=newArr[1];


        }
       return   obj;
    }

    console.log(getUrl(location.href));
</script>
</body>
</html>
展开阅读全文

没有更多推荐了,返回首页