2

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.

Alp
  • 29,274
  • 27
  • 120
  • 198
  • So user clicks element, corresponding onclick event handler changes `window.location`, and then...what do you want to happen? What do you mean by "intercepts the unload event", do you want to prevent the page unloading? I don't understand what in this scenario could be "store[d] in a variable". – nnnnnn Dec 02 '11 at 02:03
  • Yes i want to prevent that the page unloads. Instead the event object should be stored in a variable. – Alp Dec 02 '11 at 02:05
  • But why do you have code that causes the page to unload by changing `window.location` if you don't want the page to actually unload? Do you see why I'm confused? Why do you want to store the event object - what do you plan to do with it? Maybe you could update your question to explain how this would work from the user's point of view... – nnnnnn Dec 02 '11 at 02:09
  • You are right, i updated my question. – Alp Dec 02 '11 at 02:17

2 Answers2

2

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.

Community
  • 1
  • 1
jermel
  • 2,326
  • 21
  • 19
  • thanks. i do not want to use `onbeforeunload` because the dialog box cannot be automatically confirmed (or can it? as far as i know it's not possible with Selenium 2). the ajax request idea is interesting, but i do not have a webserver running yet for my application and hoped that i don't need it. – Alp Dec 02 '11 at 10:48
0

The onunload handler of body will be called while the page is getting unloaded. So you can create a javascript method to store it in a variable.

For Example: onunload="yourMethod();"

  • as far as i know the unload event is "unstoppable", so i need to do something before that happens – Alp Dec 02 '11 at 10:45