基于这里的解析函数,我们可以做出通用的解决方案:
function getParameterByName(query_string, name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(query_string);
if (results == null) return "";
else return decodeURIComponent(results[1].replace(/\+/g, " "));
}
$(".generate_url").click(function() {
var query_string = this.href.substring(this.href.indexOf("?"));
var acckey = getParameterByName(query_string, "acckey");
alert(acckey);
});
演示:http: //jsfiddle.net/dQ8Z9/
要获取查询字符串,您可以简单地使用:
$(".generate_url").click(function() {
var query_string = this.href.substring(this.href.indexOf("?") + 1);
alert(query_string);
});
演示:http: //jsfiddle.net/dQ8Z9/1/