从查询字符串中删除参数而不重新加载

时间:2022-01-04 19:41:50

Is it possible to remove parameter from query-string with js, I have tried below one unfortunately its works in browser console not in JS file.

是否可以使用js从查询字符串中删除参数,我在下面尝试过,但遗憾的是它在浏览器控制台中不能在JS文件中工作。

 var clean_uri = location.protocol + "//" +       location.host+location.pathname;
 window.history.replaceState({}, document.title, clean_uri);

what would be the best way to do it.

什么是最好的方法。

1 个解决方案

#1


1  

The reason this would only work in the browser is that the window.history API is only available in your browser. I'm not quite sure what you mean by 'from file'. But as one of the commenters pointed out the problem is in the context of where it is executed. If for instance you are executing this in a Node environment rather than a browser, there is no window.history api. Node doesn't even have a window object.

这只适用于浏览器的原因是window.history API仅在您的浏览器中可用。我不太清楚'来自档案'是什么意思。但正如其中一位评论者所指出的那样,问题在于其执行的背景。例如,如果您在节点环境而不是浏览器中执行此操作,则没有window.history api。节点甚至没有窗口对象。

Since you've added an AngularJs flag, it might be that the routing inside the app is listening to changes to the URL and will act on them. Angular may have it's own way of allowing you to change the URL but I'm not familiar enough to help with that.

由于您添加了AngularJs标志,因此应用程序内部的路由可能正在侦听对URL的更改并将对其进行操作。 Angular可能有自己的方式允许你更改URL,但我不熟悉,不能帮助你。

#1


1  

The reason this would only work in the browser is that the window.history API is only available in your browser. I'm not quite sure what you mean by 'from file'. But as one of the commenters pointed out the problem is in the context of where it is executed. If for instance you are executing this in a Node environment rather than a browser, there is no window.history api. Node doesn't even have a window object.

这只适用于浏览器的原因是window.history API仅在您的浏览器中可用。我不太清楚'来自档案'是什么意思。但正如其中一位评论者所指出的那样,问题在于其执行的背景。例如,如果您在节点环境而不是浏览器中执行此操作,则没有window.history api。节点甚至没有窗口对象。

Since you've added an AngularJs flag, it might be that the routing inside the app is listening to changes to the URL and will act on them. Angular may have it's own way of allowing you to change the URL but I'm not familiar enough to help with that.

由于您添加了AngularJs标志,因此应用程序内部的路由可能正在侦听对URL的更改并将对其进行操作。 Angular可能有自己的方式允许你更改URL,但我不熟悉,不能帮助你。