无法在 Firefox 4 中触发卸载事件
我已经在卸载事件上运行了 ajax 调用大约一年了。 它通常在 FF 和 IE 中工作,但不是 100%,我不能说它何时失败。 我通过在 bodytag 中写入来注册事件:
onunload =“....”
我在 FF4 中收到错误消息,因为卸载事件还想写入刚刚卸载的页面的 div 标签。如果目标 div 的 id 为“虚拟”,则通过使 ajax 例程不写入任何内容来修复此问题 我不是 AJAX 专家,但以下代码有效: http://yorabbit.info/e-dog.info/tmp/ajax_ex.php (链接是文本页面)
(您使用以下参数调用 ajaxfunction2:文件名、PHP 的查询字符串、更新期间在目标 div 中显示的字符串、目标 div 的名称)
我在 FF 错误控制台中没有收到任何错误消息,并且 IE9 可以正常工作。 有什么方法可以让它在FF中也能工作吗?我刚刚开始尝试FF4,但我的印象是它的工作效果不如FF3。
谢谢。
(我正在旅行,无法立即回复,但我非常感谢建议,并将在适当的时候回复)
编辑: 我最好加上这个: 我在卸载时进行的 AJAX 调用仅将一些数据(用户在页面上停留的时间)发送到 PHP-MySQL 服务器
I have run ajax-calls on the unload event for about a year.
It has generally worked in FF and IE but not to 100%, I cannot say when it has failed.
I register the event by writing in the bodytag:
onunload="...."
I got error messages in FF4 since the unload event also wanted to write in a div-tag of the page that just had unloaded. Fixed this by making the ajax-routine write nothing if the id of the target div is 'dummy'
I am no expert on AJAX, but the following code has worked:
http://yorabbit.info/e-dog.info/tmp/ajax_ex.php (the link is a text-page)
(You call ajaxfunction2 with the following arguments: filename, queryString for PHP, string to show in target div during update, name of target div)
I don't get any error messages in the FF error console and IE9 works. Is there any way I can make it work in FF too?? I have just started trying FF4, but my impression is that it works less well than in FF3.
Thanks.
(I am on a trip and ay not have the possibility to reply immediately, but I really appreciate suggestions and will reply in due course)
EDIT:
I had bettter add this:
The AJAX-call I make on unload does only send some data (how long time the user stayed on the page) to the PHP-MySQL server
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道这里发生了什么,但是 Firefox 4 对卸载的工作方式进行了显着的更改:例如,如果您在链接单击事件期间执行alert(),它将不再冻结页面,而是加载新的页面无论如何,位置。也许这就是类似的东西。
但是,如果 Ajax 调用在任何浏览器中都不同步,则永远无法保证 Ajax 调用能够完成,无论如何 - 在页面关闭之前,请求可能会也可能不会返回响应。这是否有效将取决于机会以及用户的网络速度。
首先尝试使用同步请求,如下所述: jQuery 的同步 AJAX 请求如何工作?
这通常可以保证请求返回。但是,请非常谨慎使用它 - 页面卸载时的阻塞行为可能会让用户非常恼火,甚至会冻结浏览器。
I don't know what is happening here, but Firefox 4 has made notable changes to how unloading works: For example, if you do an alert() during a link click event, it will no longer freeze the page, but load the new location anyway. Maybe this is something similar.
However, you are never guaranteed for the Ajax call to finish if it is not synchronous in any browser anyway - the request may or may not come back with a response until the page has been closed. Whether this works will be down to chance, and the user's network speed.
Try using a synchronous request first, as outlined here: How does jQuery's synchronous AJAX request work?
this will usually guarantee that the request comes back. However, use it very sparingly - blocking behaviour at page unload can be very annoying for the user, and even freeze the browser.
我建议使用 jQuery,而不是自己跟踪浏览器的更改。
解决方案:
在这里找到工作示例: http://jsfiddle.net/ezmilhouse/4PMcc/1/
假设您的内部链接是相对设置的,因此您的外部链接设置以“http”开头:
您可以通过 jQuery 事件劫持“a”标签并要求用户确认离开(如果是外部链接)。在“ok”的情况下,您启动“onleave”ajax 调用(async=true)并将用户重定向到外部链接:
I suggest to use jQuery instead of keeping track of browser changes yourself.
Solution:
Find working sample here: http://jsfiddle.net/ezmilhouse/4PMcc/1/
Assuming that your internal links are set relatively, and your external links therefore set starting with 'http':
You could hijack 'a' tags via jQuery events and ask the user to confirm the leaving (in case of external links). In 'ok' case you kick off your 'onleave' ajax call (async=true) and redirect user to external link: