I am trying to add an event to an element that fires when the page unloads. I would like to attatch the event to the element as I need to access private functions.
I decided to try and add an unload event to my element, and then call this function during the window.unload event, as the element is never passed an unload event normally.
Sadly I managed to create an infinite loop, I tried using the jQuery function 'one' in an attempt to stop the loop, and have also tried manually removing the unload even binding after the first call. Still no luck. I made a JsFiddle but decided not to link it because it will crash your browser, so here is the code.
<div id="myDiv"></div>
$('#myDiv').one('unload', function() { alert('Infinite loop incomming. Sorry :( '); });
$(window).unload(function() { $('#myDiv').unload(); });
Can anyone offer any advice or a better solution?