获取url参数
获取url参数
一、H5处理方式
/* * 获取页面url参数 * 1.用getUrlParams().name格式获取英文参数值等 * 2.获取页面url参数(含中文),用decodeURI(getUrlParams(name))格式获取 */ getUrlParam: function(name) { if (name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); var r = encodeURI(window.location.search).substr(1).match(reg); if (r != null) return unescape(r[2]); return null; } else { var url = location.search; var theRequest = new Object(); if (url.indexOf("?") != -1) { var str = url.substr(1); var strs = str.split("&"); for (var i = 0; i < strs.length; i++) { theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1]); } } return theRequest; } },
二、小程序处理方式
/** * 处理url参数, * @param name url后面的参数 * @returns Object */ const getUrlParam = name => { var url = name; var theRequest = new Object(); if (url.indexOf("?") != -1) { var str = url.substr(1); var strs = str.split("&"); for (var i = 0; i < strs.length; i++) { theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1]); } } else { var strs = url.split("&"); for (var i = 0; i < strs.length; i++) { theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1]); } } return theRequest; }