簡體   English   中英

JavaScript 從 URL 中刪除參數

[英]JavaScript remove parameter from URL

我想從 URL 中刪除參數。 我有這個 URL http://localhost/?page=test&id=190603|190629

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

單擊按鈕從 url 地址中刪除 id 並重新加載沒有刪除 ID 的地址時該怎么辦?

只是嘗試...(這是針對問題的上一個版本)

 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!')
 <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>

好吧,我有一些 lulz。

哦,並回答更新的問題:在 php 上做同樣的事情(如上)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM