JS置換URLにおけるパラメータ例
2770 ワード
<span style="color:#0000ff;">var</span> url =<span style="color:#000000;"> window.location.href;
url </span>= changeURLArg(url, "key", " "<span style="color:#000000;">),
window.location.href </span>=<span style="color:#000000;"> url;
</span><span style="color:#008000;">/*</span><span style="color:#008000;">
* url url
* arg
* arg_val
* return url url
</span><span style="color:#008000;">*/</span>
<span style="color:#0000ff;">function</span><span style="color:#000000;"> changeURLArg(url, arg, arg_val) {
</span><span style="color:#0000ff;">var</span> pattern = arg + '=([^&]*)'<span style="color:#000000;">;
</span><span style="color:#0000ff;">var</span> replaceText = arg + '=' +<span style="color:#000000;"> arg_val;
</span><span style="color:#0000ff;">if</span><span style="color:#000000;"> (url.match(pattern)) {
</span><span style="color:#0000ff;">var</span> tmp = '/(' + arg + '=)([^&]*)/gi'<span style="color:#000000;">;
tmp </span>=<span style="color:#000000;"> url.replace(eval(tmp), replaceText);
</span><span style="color:#0000ff;">return</span><span style="color:#000000;"> tmp;
} </span><span style="color:#0000ff;">else</span><span style="color:#000000;"> {
</span><span style="color:#0000ff;">if</span> (url.match('[\?]'<span style="color:#000000;">)) {
</span><span style="color:#0000ff;">return</span> url + '&' +<span style="color:#000000;"> replaceText;
} </span><span style="color:#0000ff;">else</span><span style="color:#000000;"> {
</span><span style="color:#0000ff;">return</span> url + '?' +<span style="color:#000000;"> replaceText;
}
}
</span><span style="color:#0000ff;">return</span> url + '
' + arg + '
' +<span style="color:#000000;"> arg_val;
}
</span>
転載先:https://www.cnblogs.com/kongxiaoshuang/p/9564283.html