The onBeforeUnload module provides an API to allow other modules use the onBeforeUnload event of the browser window.

This module does not provide any direct functionally, it rather provides a centralized API that other modules can use to take advantage of the onBeforeUnload event of the browser window.

Modules using the onBeforeUnload API: Dirty Forms, Modal Frame API.

Web standards?

There is no public specification on the onBeforeUnload event. It was introduced by Microsoft IE 4 and has subsequently been copied by other browsers. Currently supported by Firefox, IE4+, Google Chrome.

References:
- onBeforeUnload event (Microsoft Developer Network).
- window.onbeforeunload (Mozilla Developer Center).
- Google Chrome: sorry, I've been unable to find an official reference.

Please, let me know if there's any other browser that supports this event. Thanks! :)

Example?

Let's say your module needs to perform some check when the user leaves the page.
Ok, this module should add the onBeforeUnload API and its own javascript file
to the page with a code similar to this:

<?php
/**
* Somewhere in your PHP script mymodule.module.
*/
  // Add the onBeforeUnload API.
 
onbeforeunload_add_js();
 
// Add mymodule behavior.
 
drupal_add_js(drupal_get_path('module', 'mymodule') .'/mymodule.js');
?>

/**
* Contents of mymodule.js.
*/
Drupal.behaviors.mymodule = function(context) {
  if (!Drupal.onBeforeUnload.callbackExists('mymodule')) {
    Drupal.onBeforeUnload.addCallback('mymodule', function() {
      return 'Hello world!';
    });
  }
};

Please, check out the onBeforeUnload Example module, shipped with the API itself.

Project Information

Downloads