JavaScript从URL中删除参数

时间:2016-09-29 17:57:07

标签: javascript jquery

我想从网址中删除参数。 我有这个网址http://localhost/?page=test&id=190603|190629

<button onclick="removeId(190603)">Remove ID</button>
<button onclick="removeId(190629)">Remove ID</button>

如果点击按钮从ID地址中删除ID并重新加载地址而没有ID,我该删除该怎么办?

1 个答案:

答案 0 :(得分:-1)

只是尝试......(这是针对问题的上一版本)

&#13;
&#13;
s=location.search||$('tag').text();
m=s.match(/id=([\d|]*)/);
if(m && m[1] && m[1].indexOf('|')>-1)
m[1].split('|').forEach(function(e){
document.write('<a href=http://localhost/?page=test&id='+s.replace(new RegExp('(^|[|])'+e+'(?=$|[|])'),'')+'">Remove '+e+' id from url</a><br>')
});
//else document.write('nop!')

document.write('<hr>');

// single id test:
s='?page=test&id=190603'
m=s.match(/id=([\d|]*)/);
if(m && m[1] && m[1].indexOf('|')>-1)
m[1].split('|').forEach(function(e){
document.write('<a href=http://localhost/?page=test&id='+s.replace(new RegExp('(^|[|])'+e+'(?=$|[|])'),'')+'">Remove '+e+' id from url</a><br>')
});
else document.write('nop!')

document.write('<hr>');

// no id test:
s='?page=test'
m=s.match(/id=([\d|]*)/);
if(m && m[1] && m[1].indexOf('|')>-1)
m[1].split('|').forEach(function(e){
document.write('<a href=http://localhost/?page=test&id='+s.replace(new RegExp('(^|[|])'+e+'(?=$|[|])'),'')+'">Remove '+e+' id from url</a><br>')
});
else document.write('nop!')
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
location.search=<tag color="red">?page=test&amp;id=190603|764583|87436835|846345</tag><br>
&#13;
&#13;
&#13;

好的,我得到了一些lulz。

Ow,并回答更新的问题:在php上做同样的事情(如上所述)

相关问题