[英]“if (location.href != example.com/page1 || example.com/page2)” Change CSS element
[英]JavaScript: how to redirect from “example.com/page1” to “example.com/page2” in same domain?
I want a JavaScript code that redirects https://www.example.com/collections/all?sort_by=best-selling
to https://www.example.com/collections/all
in the same domain name using JavaScript.
试试这个,它会重定向到当前域的/collections/all
:
if (location.href.includes('/collections/all?sort_by=best-selling ')
window.location.replace("/collections/all");
你也可以使用
window.location.assign
查看 window.location.assign 和 window.location.replace 之间的区别https://stackoverflow.com/a/4505846/48回答
如果您使用window.location.assign("yoururl")
它会导致加载一个新文档。 如果您使用window.location.replace("yoururl")
它会替换当前文档并用该 URL 替换当前历史记录,这样您就无法将 Z34D1F91FB2E514B8576FAB1A75A76 加载回之前的文档。
参考: https://developer.mozilla.org/fr/docs/Web/API/Location/assign参考: https://developer.mozilla.org/fr/docs/Web/API/Location/replace
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.