今天同事用的以前的获取url地址参数获取不到.以前的方法失效了.后面发现是正则表达式bug:
第一种获取方法(针对普通情况的一般够用):
function getQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); var reg_rewrite = new RegExp("(^|/)" + name + "/([^/]*)(/|$)", "i"); var r = window.location.search.substr(1).match(reg); var q = window.location.pathname.substr(1).match(reg_rewrite); if(r != null){ return unescape(r[2]); }else if(q != null){ return unescape(q[2]); }else{ return null; } }
第二种获取方法:
这样的url地址获取不到:file:///Users/wanghong/work/ziku/youle/dist/index.html?sourceType=Tsingtao_beer_activity#/welcome?toPath=file%3A%2F%2F%2FUsers%2Fwanghong%2Fwork%2Fziku%2Fyoule%2Fdist%2Findex.html%3FsourceType%3DTsingtao_beer_activity%23%2F
/** * 获取指定的URL参数值 * URL:http://www.quwan.com/index?name=tyler * 参数:paramName URL参数 * 调用方法:getParam("name") * 返回值:tyler */ function getParam(paramName) { paramValue = "", isFound = !1; if (this.location.search.indexOf("?") == 0 && this.location.search.indexOf("=") > 1) { arrSource = unescape(this.location.search).substring(1, this.location.search.length).split("&"), i = 0; while (i < arrSource.length && !isFound) arrSource[i].indexOf("=") > 0 && arrSource[i].split("=")[0].toLowerCase() == paramName.toLowerCase() && (paramValue = arrSource[i].split("=")[1], isFound = !0), i++ } return paramValue == "" && (paramValue = null), paramValue }
大家都尝试一下.元宵节快乐