用下面这个函数:
/** * 获取到本页浏览器的url参数中的值. * 注意:直接获取到的url实际上是浏览器encode编码以后的,需要用decodeuri解码才是中文 * @param key url参数中的键 * @returns {*} 返回该key对应的值 */ function getQueryString(key){ var reg = new RegExp("(^|&)"+key+"=([^&]*)(&|$)"); var result = window.location.search.substr(1).match(reg); return result?decodeURIComponent(result[2]):null; }
一个注意事项,浏览器url是编码过的,如果你alert(window.location.search),可能会看到乱码。不能使用unescape(),因为已经被淘汰,而使用decodeURI或者decodeURiComponent