The new GoogleMaps api defines a function to be called onunload of a page in order to prevent memory leaks.
At first I thought a "hook_onunload" function was missing from Drupal core, but this seems to have been replaced by a javascript addLoadEvent function in misc/drupal.js.
A addUnloadEvent function can be added as follows (modelled after the addLoadEvent function):

/**
* Adds a function to the window onunload event
*/
function addUnloadEvent(func) {
  var oldOnunload = window.onunload;
  if (typeof window.onunload != 'function') {
    window.onunload = func;
  }
  else {
    window.onunload = function() {
      oldOnunload();
      func();
    }
  }
}

Comments

jpulles’s picture

Sorry for the typo. I meant that the 'hook_onload' function was replaced by addLoadEvent.

Ignas’s picture

Dont work.
Placed this function into drupal.js, it 'adds' my function to unload list but unload function never fired.
Try yourself (js code, dont work):

function GUnload2()	{	
	alert('My unload function');	
}
addUnloadEvent(GUnload2);  

Not nice solution is to add unload event into page.tpl.php: '' . (Works)
We need this function in drupal core! anybody hears?

magico’s picture

Title: addUnloadEvent function is needed » How to add unload events with the new jQuery stuff
Category: bug » support

addUnloadEvent was replaced by the jQuery $(document).ready().

I think something like this could work (let's call javascript experts)

if (Drupal.jsEnabled) {
  $(document).ready(Drupal.collapseAutoAttach);
  $(document).unload(function() { alert("Unloading"); });
}

http://www.visualjquery.com/

brst t’s picture

Version: x.y.z » 5.0-beta2

Can this be done?

I'm currently using the method I found here : http://drupal.org/node/84972

Which I think would be comparatively clunky to a jQuery solution.

brst t’s picture

Hm. Turns out adding this bit of code to the end of my script worked fine.

window.onload = load;
window.onunload = GUnload; 

And, in keeping w/ the jQuery issue/subject, here, I did find a couple decent jQuery plugins for googlemaps:

http://www.dyve.net/jquery/?googlemaps

And as posted to the page: http://olbertz.de/jquery/googlemap.html

I don't quite have a very good handle on jQuery, yet...

robmilne’s picture

I want to have an unload event for WebFM since it creates many event closures on the DOM. Unfortunately I had to monkey with the garland theme to make it work. See http://vera-ikona.com/node/47

ricabrantes’s picture

Status: Active » Closed (fixed)

resolved issue, Closed