JavaScript:如何从同一域中的"example.com/page1"重定向到"example.com/page2"?



我想要一个JavaScript代码,使用JavaScript将https://www.example.com/collections/all?sort_by=best-selling重定向到同一域名中的https://www.example.com/collections/all

你也可以使用

window.location.assign

查看 window.location.assign 和 window.location.replace 之间的区别,在 https://stackoverflow.com/a/4505846/4987870 回答

试试这个,它会重定向到当前域/collections/all

if (location.href.includes('/collections/all?sort_by=best-selling ') 
window.location.replace("/collections/all");

如果使用window.location.assign("yoururl")则会导致加载新文档。如果您使用window.location.replace("yoururl")它会替换当前文档,并将当前历史记录替换为该 URL,因此您无法返回到加载的上一个文档。

参考: https://developer.mozilla.org/fr/docs/Web/API/Location/assign 编号 : https://developer.mozilla.org/fr/docs/Web/API/Location/replace

相关内容

最新更新