1. //替換指定傳入參數的值,paramName為參數,replaceWith為新值
function replaceParamVal(oUrl,paramName, replaceWith) {
var re = eval('/(' + paramName + '=)([^&]*)/gi');
var nUrl = oUrl.replace(re, paramName + '=' + replaceWith);
return nUrl;
}
2.//向URL中添加參數,如果參數存在替換參數的值
function UpdateUrlWithParam(url, key, value) {
var retUrl = url;
if (retUrl.indexOf("?") == -1) {
retUrl += "?" + key + "=" + value;
}
else {
if (retUrl.indexOf("&" + key + "=") == -1) {
if (retUrl.indexOf("?" + key + "=") == -1)
retUrl += "&" + key + "=" + value;
else
retUrl = retUrl.replace(eval('/(' + key + '=)([^&]*)/gi'), "?" + key + "=" + value);
} else {
retUrl = retUrl.replace(eval('/(' + key + '=)([^&]*)/gi'), "&" + key + "=" + value);
}
}
return retUrl;
}
3.//得到url裡參數的值
function getQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]); return null;
}