javascript:window.location 会发出任何警告吗?
如果我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不,您可以自行重定向窗口位置。您无法自动执行的唯一类似相关操作是单击具有事件的用户的链接。
您可能正在考虑这种类型的通知:
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:
不会。您不会收到任何警告(就像关闭窗口时收到的警告一样)。我从未见过 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 :)
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.
设置 window.location 不应导致任何重定向问题。但是,如果存在哈希值,则某些浏览器可能会以不同的方式处理它。
来自 MDN 规范:
location
和location.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:
There shouldn't be any difference between
location
andlocation.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