---
layout: page
title: "JavaScript register_shutdown_function function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/register_shutdown_function:810
- /functions/view/register_shutdown_function
- /functions/view/810
- /functions/register_shutdown_function:810
- /functions/810
---
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's register_shutdown_function

{% codeblock funchand/register_shutdown_function.js lang:js https://raw.github.com/kvz/phpjs/master/functions/funchand/register_shutdown_function.js raw on github %}
function register_shutdown_function (cb) {
  // From: http://phpjs.org/functions
  // +   original by: Brett Zamir (http://brett-zamir.me)
  // *     example 1: register_shutdown_function(function(first, middle, last) {alert('Goodbye '+first+' '+middle+' '+last+'!');}, 'Kevin', 'van', 'Zonneveld');
  // *     returns 1: 'Goodbye Kevin van Zonneveld!'
  var args = [],
    _addEvent = function (el, type, handler, capturing) {
      if (el.addEventListener) { /* W3C */
        el.addEventListener(type, handler, !! capturing);
      } else if (el.attachEvent) { /* IE */
        el.attachEvent('on' + type, handler);
      } else { /* OLDER BROWSERS (DOM0) */
        el['on' + type] = handler;
      }
    };

  args = Array.prototype.slice.call(arguments, 1);
  _addEvent(this.window, 'unload', function () {
    cb.apply(null, args);
  }, false);
}
{% endcodeblock %}

 - [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/funchand/register_shutdown_function.js)

Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are 
the closest match to this hashtable-like data structure. 

Please also note that php.js offers community built functions and goes by the 
[McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online 
functions that are far from perfect, in the hopes to spark better contributions. 
Do you have one? Then please just: 

 - [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/funchand/register_shutdown_function.js)

### Example 1
This code
{% codeblock lang:js example %}
register_shutdown_function(function(first, middle, last) {alert('Goodbye '+first+' '+middle+' '+last+'!');}, 'Kevin', 'van', 'Zonneveld');
{% endcodeblock %}

Should return
{% codeblock lang:js returns %}
'Goodbye Kevin van Zonneveld!'
{% endcodeblock %}


### Other PHP functions in the funchand extension
{% render_partial _includes/custom/funchand.html %}
