获取url参数

    xiaoxiao2021-03-25  71

    //1、split拆分法

    function getQueryStringArgs() {

            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),         //保存数据的对象         args = {},         //取得每一项         items = qs.length ? qs.split("&") : [],         item = null,         value = null,             //在for循环中使用             i = 0,             len = items.length;         for ( i =  0; i < len ;i++){             item = items[i].split("=");             name = decodeURIComponent(items[0]);             value = decodeURIComponent(items[1]);             if(name.length){                 args[name] = value;             }         }         return args;

        }

    //2、正则法 function GetQueryString(name) {   var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");   var r = window.location.search.substr(1).match(reg);  //获取url中"?"符后的字符串并正则匹配 var context = "";   if (r != null)   context = r[2];   reg = null;   r = null;   return context == null || context == "" || context == "undefined" ? "" : context;   }

    转载请注明原文地址: https://ju.6miu.com/read-38545.html

    最新回复(0)