window.onbeforeunload无法在iPad上运行? [英] window.onbeforeunload not working on the iPad?

查看:108
本文介绍了window.onbeforeunload无法在iPad上运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道iPad上是否支持 onbeforeunload 事件和/或是否有不同的使用方式?

Does anyone know if the onbeforeunload event is supported on the iPad and/or if there's a different way to use it?

我已经尝试了很多东西,似乎从未在iPad(Safari浏览器)上触发 onbeforeunload 事件。

I've tried pretty much everything, and it seems like the onbeforeunload event is never triggered on the iPad (Safari browser).

具体来说,这就是我尝试过的:

Specifically, this is what I've tried:


  • window.onbeforeunload = function(event){event.returnValue ='test'; }

  • window.onbeforeunload = function(event){return'test'; }

  • (以上两者一起)

  • window.onbeforeunload = function(event ){alert('test')'; }

  • (所有上述功能但在< body onbeforeunload =...>

  • window.onbeforeunload = function(event) { event.returnValue = 'test'; }
  • window.onbeforeunload = function(event) { return 'test'; }
  • (both of the above together)
  • window.onbeforeunload = function(event) { alert('test')'; }
  • (all of the above functions but inside <body onbeforeunload="...">

所有这些都适用于PC上的FF和Safari,但不适用于iPad。

All of these work on FF and Safari on the PC, but not on the iPad.

此外,我在加载页面后完成了以下操作:

Also, I've done the following just after loading the page:

alert('onbeforeunload' in window);
alert(typeof window.onbeforeunload);
alert(window.onbeforeunload);

分别为:


  • true

  • object

  • null

  • true
  • object
  • null

因此,浏览器确实拥有该属性,但由于某种原因它不会被解雇。

So, the browser does have the property, but for some reason it doesn't get fired.

我尝试离开页面的方法是点击后退和前进按钮,在顶部栏中进行谷歌搜索,更改地址栏中的位置,然后点击书签。

The ways I try to navigate away from the page are by clicking the back and forward buttons, by doing a google search in the top bar, by changing location in the address bar, and by clicking on a bookmark.

有没有人知道什么怎么回事?我非常感谢任何意见。

Does anyone have any idea about what's going on? I'd greatly appreciate any input.

谢谢

推荐答案

我发现onunload()事件确实触发了。这种行为有点奇怪;你在回调函数中拥有的任何东西实际上都是在新页面加载到后台后运行的(你无法告诉它已经加载了,但是服务器日志记录会显示它已经加载)。

I have found that the onunload() event does fire. It's behavior is somewhat odd; whatever you have in your callback function attached to the event is actually run after the new page has loaded in the background (You can't tell it's loaded yet, but server logging will show that it has).

更奇怪的是,如果你的onunload()中有一个confirm()调用,并且用户点击了链接去了其他地方,你就可以了。但是,如果用户关闭了iPad Safari浏览器选项卡,则会触发onunload()事件,但您的confirm()将作为响应隐式取消。

More oddly, if you have a confirm() call in your onunload(), and the user has clicked a link to go somewhere else, you are in business. If, however, the user closes the iPad Safari browser tab, the onunload() event will fire, but your confirm() will have an implicit cancel as response.

这篇关于window.onbeforeunload无法在iPad上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆