拦截JavaScript卸载事件 [英] Intercept JavaScript unload events

查看:139
本文介绍了拦截JavaScript卸载事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个带有任意数量元素的网页,附有事件处理程序,我们假设 onclick 。在处理程序中,触发了一个卸载事件,例如通过分配 window.location



任务:我需要以检测事件处理程序是否将卸载页面。我如何添加另一个拦截卸载事件的事件处理程序,并将其存储在变量中,而不是触发它?



更新



这是一个解释,由于混乱。我写了一个使用Selenium 2的Java应用程序来提取有关用户事件的所有信息。这是很容易完成,除了卸载。我必须等待拉,才能最终卸载页面。

解决方案

如果你需要防止页面卸载看这个帖子:
我如何在JS中停止卸载页面(导航)



如果您需要跟踪页面卸载,可以使窗口的卸载事件(您的页面将不会退出,直到此请求完成)



如果您需要跟踪哪个自定义事件处理程序卸载页面,您将必须跟踪当前的事件处理程序,然后执行上述解决方案之一。


Assumed that i have web pages with an arbitrary amount of elements which have an event handler attached, let's say onclick. In the handler an unload event is triggered, for example by assigning window.location.

The task: I need to detect if an event handler will unload the page. How can i add another event handler which intercepts the unload event and stores it in a variable instead of triggering it?

Update

Here is an explenation, due to confusion. I have written a Java application that uses Selenium 2 to pull all information about user events. This is easily done, except for unloads. I have to wait for the pull before i can finally unload the page.

解决方案

if you need to prevent page unload see this post: How do I stop a page from unloading (navigating away) in JS?

If you need to track page unloads, you can make a synchronous xhr request (tip set async=false) during your window's unload event (your page will not exit until this request finishes)

if you need to track which custom event handlers unload the page, you will have to track the current event handler and then do one of the above mentioned solutions.

这篇关于拦截JavaScript卸载事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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