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.csdn.com/?id=3&ttt=3
par is the parameter name, par_value is the value of the parameter to be changed, the call result is as follows:
var url = window.location.href;
changeURLPar(url, 'id', 99); // http://www.csdn.com/?id=99&ttt=3
changeURLPar(url, 'haha', 33); // http://www.csdn. com/?id=99&ttt=3&haha=33