用javascript替换URL中的参数值

时间:2023-03-10 05:33:13
用javascript替换URL中的参数值
<script>
function changeUrlArg(url, arg, val){
var pattern = arg+'=([^&]*)';
var replaceText = arg+'='+val;
return url.match(pattern) ? url.replace(eval('/('+ arg+'=)([^&]*)/gi'), replaceText) : (url.match('[\?]') ? url+'&'+replaceText : url+'?'+replaceText);
} document.write(changeUrlArg('http://www.t.com/test.php?class_id=3&id=2', 'id', 4));
</script>