This article custom JS function can dynamically add URL parameters, modify the value of the URL parameters, the specific implementation of the following, interested friends can refer to the hope for everyone to help
Whether front-end development or background design, many times developers need to get information about the current or target URLs. This existing built-in object attribute can be invoked directly (here is the reference code to get the current page)
Copy CodeThe code is as follows:
<script type= "Text/javascript" >
Thisurl = document. URL;
Thishref = Document.location.href;
Thissloc = Self.location.href;
Thisdloc = document.location;
Strwrite = "Thisurl: [" + Thisurl + "]<br>"
Strwrite + = "Thishref: [" + Thishref + "]<br>"
Strwrite + = "Thissloc: [" + Thissloc + "]<br>"
Strwrite + = "Thisdloc: [" + Thisdloc + "]<br>"
document.write (Strwrite);
</script>
But sometimes we need to change the current URL of the parameters/parameter values, at this time what people will handle it? In general, you should make a change to a parameter by getting the information of all the parameters and then depending on the actual requirements. That's right! Based on this principle, let's share with you today the functional functions that the individual encapsulates in development.
=============== change the value of the URL's parameter ================
Copy CodeThe code is as follows:
function Changeurlparm (turl,parm,pvalue,clearparm) {
Turl: Web site
Parm: Parameters
Pvalue: Parameter values
Clearparm: Parameters to clear
var url,parms,parmsarr,isexist;
var newurl = Turl;//window.location.href
Isexist = false;
With (Turl) {
if (indexOf ('? ') >0) {
URL = substr (0,indexof ('? ')); /does not contain parameters
Parms = substr (indexOf ('? ') +1,LENGTH);//Parameter
}
else{
URL = Turl;
Parms = ';
}
}
if (parms!= ') {
var i;
Parmsarr = Parms.split ("&");
for (i=0;i<=parmsarr.length-1;i++) {
if (String (parm). toUpperCase () ==string (parmsarr[i].split ("=") [0]). toUpperCase ()) {//original parameter parm change its value
Parmsarr[i] = parm + "=" + pvalue;
Isexist = true;
if (String (clearparm) = = "") {
Break
}
}
else if ((clearparm)!= "") && (String (clearparm). toUpperCase () ==string (parmsarr[i].split ("=") [0]). toUpperCase ()) {//Remove the value of the parameter Clearparm
Parmsarr[i] = clearparm + "=";
}
}
for (i=0;i<=parmsarr.length-1;i++) {
if (i==0) {
Parms = Parmsarr[i];
}
else{
Parms = Parms + "&" + parmsarr[i];
}
}
Newurl = URL + "?" + Parms;
if (! Isexist) {
Newurl = Newurl + "&" + parm + "=" + pvalue;
}
}
else{
Newurl = URL + "?" + parm + "=" + pvalue;
}
return newurl;
}
In fact, this function in the practical application is very good, in the paging jump, multiple conditions query search and other functions are particularly prominent.