Home  >  Article  >  Web Front-end  >  js changes the address bar URL parameters to solve the url parameter problem_Basic knowledge

js changes the address bar URL parameters to solve the url parameter problem_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 17:46:301245browse

Nowadays, when making web pages, we often encounter the problem of dealing with address bar parameters, but I always do it based on feeling. Later, there are many loopholes and I don’t know where to start. Therefore, I made a special method to modify the address bar parameters. This time, You don’t have to worry anymore, haha!

The following is the method I compiled, it is very simple to use:

Copy code The 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, such as http://www.huistd.com/?id=3&ttt=3
par is the parameter name, par_value is the value of the parameter to be changed. The calling result is as follows:
changeURLPar(test, 'id', 99); // http://www.huistd.com/?id=99&ttt=3
changeURLPar(test, 'haha', 33); // http://www.huistd.com/?id=99&ttt=3&haha=33
How about it, it’s very useful! Ha ha
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn