setQueryStringParameter
= function
(uri, key, value) {
var re = new RegExp("([?|&])" + key + "=.*?(&|$)", "i");
separator = uri.indexOf('?') !== -1 ? "&" : "?";
if (uri.match(re)) {
return uri.replace(re, '$1' + key + "=" + value + '$2');
}
else {
return uri + separator + key + "=" + value;
}
};
getQueryStringParameterByName
= function
(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
};
Un esempio per il suo utilizzo:
setQueryString
= function
() {
var txt = getQueryStringParameterByName("test");
if (!txt)
txt = 0;
txt = parseInt(txt);
txt++;
document.location =
setQueryStringParameter(document.location.href, "test", txt);
};
<div>
<input type="button" onclick="setQueryString()" title="setQueryString" value="setQueryString" />
</div>
Nessun commento:
Posta un commento