zno2

在 html 通过js 获取查询参数

 

file:///E:/test.html?a=1&b=2

<script type="text/javascript">
    var getParameters = function() {
        var search = window.location.search;
        var obj = {};
        if (search != '') {
            // window.location.search '' or start with '?'
            search = search.substring(1);
            var keypairArray = search.split('&');
            for (i in keypairArray) {
                keypair = keypairArray[i];
                partArray = keypair.split('=');
                if(partArray.length == 2){
                    obj[partArray[0]] = partArray[1];
                }
            }
        }
        return obj;
    }

console.dir(getParameters());

</script>

 

 

结果:

 

posted on 2023-08-25 16:27  zno2  阅读(130)  评论(0编辑  收藏  举报

导航