javascript:window.location 会发出任何警告吗?

发布于 2025-01-02 12:28:01 字数 181 浏览 0 评论 0原文

如果我使用 window.location = ""; 重定向用户,浏览器是否会向用户发出警告消息(因为页面在未经用户同意的情况下重定向)?

我应该使用 window.location = "" 还是 window.location.href = ""

If I use window.location = ""; to redirect a user, will any browsers give the user a warning message (since the page is redirecting without the user's consent)?

Should I use window.location = "" or window.location.href = ""?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

酒废 2025-01-09 12:28:01

不,您可以自行重定向窗口位置。您无法自动执行的唯一类似相关操作是单击具有事件的用户的链接。

您可能正在考虑这种类型的通知:

window.onbeforeunload = function() {
    return "Are you sure you want to navigate away?";
}

No you can redirect the window location on your own. The only similarly related thing you can't do automatically is click a link for the user with an event.

You might be thinking of this type of notice:

window.onbeforeunload = function() {
    return "Are you sure you want to navigate away?";
}
萌辣 2025-01-09 12:28:01

不会。您不会收到任何警告(就像关闭窗口时收到的警告一样)。我从未见过 location 和 location.href 有任何区别,但我使用最后一个:)

No. You will not get any warning (like you do if you close the window). And I have never seen any difference in location and location.href, but I use the last :)

岁月无声 2025-01-09 12:28:01

AFAIK window.location 和 window.location.href 应该几乎是等价的。

我已经有一段时间没有使用它了,但我不记得在离开页面之前见过任何提示。大多数时候,当我收到提示时,这是因为我明确地进行了确认,例如单击链接删除某些内容时。

AFAIK window.location and window.location.href should be pretty much eqivalent.

It's been a while since I've used it, but I don't remember ever seeing a prompt before leaving the page. Most of the time when I have been prompted it's been because I explicitly put a confirmation in, such as when clicking a link to delete something.

只为一人 2025-01-09 12:28:01

设置 window.location 不应导致任何重定向问题。但是,如果存在哈希值,则某些浏览器可能会以不同的方式处理它。

来自 MDN 规范:

注意:上面的例子适用于不需要保留window.location.hash的情况。但是,在基于 Gecko 的浏览器中,以这种方式设置 window.location.pathname 将删除 window.location.hash 中的任何信息,而在 WebKit(以及可能的其他浏览器)中,设置路径名不会更改哈希值。如果您需要更改路径名但保持哈希值不变,请使用replace() 方法,该方法应该在浏览器中保持一致。

locationlocation.href 之间不应该有任何区别,因为每当修改 location 对象的属性时,都会使用 URL 加载文档,就好像 window 一样。已使用修改后的 URL 调用 location.assign()。

来源:https://developer.mozilla.org/en/DOM/window.location

Setting window.location shouldn't cause any issues with redirection. However, if there is a hash, then some browsers may deal with it differently.

From the MDN spec:

Note: The example above works in situations where window.location.hash does not need to be retained. However, in Gecko-based browsers, setting window.location.pathname in this manner will erase any information in window.location.hash, whereas in WebKit (and possibly other browsers), setting the pathname will not alter the the hash. If you need to change pathname but keep the hash as is, use the replace() method instead, which should work consistently across browsers.

There shouldn't be any difference between location and location.href since whenever a property of the location object is modified, a document will be loaded using the URL as if window.location.assign() had been called with the modified URL.

Source: https://developer.mozilla.org/en/DOM/window.location

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文