Modify URL parameters in the address bar in js to solve the url parameter problem

Source: Internet
Author: User

Currently, webpage processing often involves processing address bar parameters, but it has always been done by feeling, and I don't know where to change it. Therefore, I made a special method to modify the parameters in the address bar, so I don't have to worry about it anymore. Haha!

The following is my compilation method, which is easy to use.:
Copy codeThe Code is as follows:
Function changeURLPar (destiny, par, par_value)
{
Var pattern = par + '= ([^ &] *)';
Var replaceText = par + '=' + par_value;
If (destiny. match (pattern ))
{
Var tmp = '/\' + par + '= [^ &] */';
Tmp = destiny. replace (eval (tmp), replaceText );
Return (tmp );
}
Else
{
If (destiny. match ('[\?] '))
{
Return destiny + '&' + replaceText;
}
Else
{
Return destiny + '? '+ ReplaceText;
}
}
Return destiny + '\ n' + par +' \ n' + par_value;
}

Destiny is the target string, for example, http://www.huistd.com /? Id = 3 & ttt = 3
Par is the parameter name and par_value is the value to be changed. The call result is as follows:
ChangeURLPar (test, 'id', 99); // http://www.huistd.com /? Id = 99 & ttt = 3
ChangeURLPar (test, 'hahaha', 33); // http://www.huistd.com /? Id = 99 & ttt = 3 & haha = 33
How are you doing? It's quite helpful! Haha

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.