JS Custom Function function realizes dynamic add URL parameter modify URL parameter value

Source: Internet
Author: User
Tags split
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.