This article is mainly about the JavaScript redirect URL parameters of two ways, the following is not much to say, see the sample code directly.
One, the character splicing form
function Seturi (Para, Val) {var strnewurl = new String ();
var strurl = new String ();
var url = window.location.href;
strURL = window.location.href; if (Strurl.indexof ("?")!=-1) {strURL = Strurl.substr (Strurl.indexof ("?") + 1); Gets the parameter if (Strurl.tolowercase (). IndexOf (Para.tolowercase ()) = = 1) {//If no parameters are found, direct assignment Strnewurl = URL +
"&" + para + "=" + val;
Window.location.href = Strnewurl;
else {var aparam = Strurl.split ("&"); for (var i = 0; i < aparam.length i++) {if aparam[i].substr (0, aparam[i].indexof ("=")). toLowerCase () = =
Para.tolowercase ()) {Aparam[i] = aparam[i].substr (0, aparam[i].indexof ("=")) + "=" + val;
} strnewurl = url.substr (0, Url.indexof ("?") + 1) + Aparam.join ("&");
Window.location.href = Strnewurl;
} else {strURL = "" + para + "=" + val; WindoW.location.href = strURL; }
}
II, using regular
Use regular function Setpara (Para, Val) {var newpar = "";
var url = window.location.href;
var pars = location.search.substring (1);
var reg = new RegExp ("(^|)" + para + "= ([^&]*) (|$)"); if (Reg.test (pars)) {//have the required parameters para var p1 = pars.split (para) [0]; productid=100857&count=1& var p2 = pars.split (para) [1]; =75825&coupval=1.5&addressid=358&invoiceid=1245&invoicename=jesse if (p2.indexOf ("&") >
-1 {var P3 = p2.split ("&") [0];
if (P3 = = "+ val +" ") {return false;
} Newpar = p1 + para + ' = ' + val + ' & ' + (P2.split (p3)) [1];
else {if (p1) {newpar = p1 + para + ' = ' + val;
else {Newpar = para + ' = ' + val;
}} else {if (Url.indexof ("?") = = 1) {Newpar = pars + "&" + para + "=" + val; else {Newpar = pars +"&" + para + "=" + val; } window.location.href = Location.href.split ('? ')
[0] + "?" + Newpar; }
Call:
<script>
window.onload = function () {
var btn = document.getElementById ("Btnclick");
Btn.onclick = function () {
//URL address: Http://localhost:54714/testuri.aspx?productID=100857&count=1&co Upresid=12785&coupval=1.5&openid=12456&addressid=358&invoiceid=&invoicename=
//setUri ( "Coupresid", 0);
Setpara ("Coupresid", 0);
}
</script>
<input type= "button" id= "Btnclick" value= "Redefine parameters"/>
Summarize
The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.