封装方法如下:
function getHash(key, url) { var hash; if (!!url) { hash = url.replace(/^.*?[#](.+?)(?:\?.+)?$/, "$1"); hash = (hash == url) ? "" : hash; } else { hash = self.location.hash; } hash = "" + hash; hash = hash.replace(/^[?#]/, ‘‘); hash = "&" + hash; var val = hash.match(new RegExp("[\&]" + key + "=([^\&]+)", "i")); if (val == null || val.length < 1) { return null; } else { return decodeURIComponent(val[1]); } }
调用如下:
http://localhost/myPage/home.html#page=sharePassport&type=preview getHash("type")的值就是preview,以&分开参数,也可以修改里面的分隔符,设置譬如"?"之类的符号分隔。
原文地址:https://www.cnblogs.com/sakura-lifangfang/p/9634083.html
时间: 2024-10-13 06:31:17