/** * 获取url的参数,并转成对象 */ function getUrlParamJson() { var url = window.location.href; if (url.indexOf("?") == -1) { return {}; } var obj = {}; url = url.substring(url.indexOf("?") + 1); var arr = url.split('&'); var len = arr.length; for (var a = 0; a < len; a++) { var nv = arr[a].split('='); obj[nv[0]] = unescape(nv[1]); } return obj; }
/** * param 将要转为URL参数字符串的对象 * key URL参数字符串的前缀 * encode true/false 是否进行URL编码,默认为true * return URL参数字符串 * var obj={name:'tom','class':{className:'class1'},classMates:[{name:'lily'}]}; output: &name=tom&class.className=class1&classMates[0].name=lily output: &stu.name=tom&stu.class.className=class1&stu.classMates[0].name=lily */ function objectToParam(param, key, encode) { if (param == null) return ''; var paramStr = ''; var t = typeof (param); if (t == 'string' || t == 'number' || t == 'boolean') { paramStr += '&' + key + '=' + ((encode == null || encode) ? escape(param) : param); } else { for (var i in param) { var k = key == null ? i : key + (param instanceof Array ? '[' + i + ']' : '.' + i); paramStr += objectToParam(param[i], k, encode); } } return paramStr; };
标签:return,name,url,param,js,参数,key,var From: https://www.cnblogs.com/louby/p/17485206.html