我知道如何更改网址,但我可以更改整个链接吗?例如,如果用户打开 stackoverflow.com,它将向他显示 exmaple.com。这是我迄今为止仅用于更改 URL 的代码:
window.history.pushState(“object or string”, “Title”, “/new-url”);
最佳答案
这是不可能的。
pushState
允许您更新 URL 的本地部分,以便您可以向使用 JavaScript 零散构建的页面提供直接 URL。
它不会让您假装成不同的网站,这将是一个严重的安全风险(因为它会成为一种有效的网络钓鱼技术)。这就是为什么the specification明确禁止:
Compare the resulting parsed URL to the result of applying the URL parser algorithm to the document's address. If any component of these two URLs differ other than the path, query, and fragment components, then throw a SecurityError exception and abort these steps.
关于javascript - 当页面打开时,如何更改网址栏中的整个链接而不仅仅是 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36404741/