从当前URL中删除GET参数


Delete GET params from current URL

当前我正在使用javascript来确认表单。

echo "<script>
var x = document.URL;
var r = confirm('Are you sure?');
if (r == true) {
window.location.search += '&answer=1'; 
 </script>";

所以它添加了get-param"answerswer",在我的php文件中我检查了这个数字(如果回答"Cancel",它将返回0),但我需要在检查后从我的url中删除这个参数,但我没有找到任何方法。

在这个过程中,我的URL应该是这样的。

starting url = www.myurl.com

用户发送表单,并对问题回答"是"

current url = www.myurl.com?answer=1;

php检查get变量,如果答案为"是",则在完成函数后修改url

url after php function = www.myurl.com

您可以使用它来删除参数:

window.location.search = '';

要在不使用get参数的情况下获取当前页面URL:

console.log(window.location.origin + window.location.pathname);