function query(key, url) {
url = url || location.href;
var reg = new RegExp('(\\?|&|#|&)' + key + '=([^?&#]*)');
var result = url.match(reg);
return result ? decodeURIComponent(result[2]) : '';
}
給 url 追加請求參數(shù)
function addParameter(key, value, url) {
url = url || location.href;
var reg = new RegExp('([?&]' + key + '=)([^?&#]*)');
if (reg.test(url)) {
return url.replace(reg, '$1' + encodeURIComponent(value));
}
var conn = url.indexOf("?") == -1 ? "?" : "&";
var param = conn + key + "=" + encodeURIComponent(value);
if (url.indexOf('#') === -1) {
url += param;
}
else {
url = url.replace(/#/, param + "#");
}
return url;
}