function use: as title, 1. Resolve parameters from URL, 2. Override parameter values in URL
The test is included as shown in the following code. You can copy directly into the browser, enter the test address: localhost:xxx?a=1&b=2&c=3, run the view results in the console:
-test//-get from current Locationvar URL = document.location.href;//-extract param from Urlconsole.log (geturlparam (URL, "a") = = "1"), Console.log (Geturlparam (URL, "b") = = "2"), Console.log (Geturlparam (URL, "c") = = "3");//-Rewrite Urlurl = Seturlparam (URL, "a", 3); url = seturlparam (URL, "B", 1); url = seturlparam (URL, "C", 2); Console.log (Geturlparam (URL, "a") = = "3"), Console.log (Geturlparam (URL, "b") = = "1"), Console.log (Geturlparam (URL, "c") = = "2");//-helper functions function Geturlparam (location,name) {var url = location; var splitindex = Url.indexof ("?") + 1; var paramstr = Url.substr (Splitindex, url.length); var arr = paramstr.split (' & '); for (var i = 0; i < arr.length; i++) {var kv = arr[i].split (' = '); if (kv[0] = = name) {return kv[1]; }} return ""; } function Seturlparam (Location,name, value) {var url = location; var splitindex = Url.indexof ("?") + 1; var paramstr = Url.substr (Splitindex, url.length); var newurl = url.substr (0, Splitindex); -if exist, replace var arr = paramstr.split (' & '); for (var i = 0; i < arr.length; i++) {var kv = arr[i].split (' = '); if (kv[0] = = name) {Newurl + = Kv[0] + "=" + value; } else {if (kv[1]! = undefined) {Newurl + = Kv[0] + "=" + kv[1]; }} if (i! = arr.length-1) {Newurl + = "&"; }}//-If new, add if (Newurl.indexof (name) < 0) {Newurl + = Splitindex = = 0? "?" + name + "=" + Value: "&" + name + "=" + value; } return Newurl; }
Running results in IE:
Get the query string in JS and the function to rewrite the URL