Search code examples
javascriptzend-debugger

To much recursion, what does this code do?


I've implemented part of this script to help me with debugging

var ZFDebugLoad = window.onload;
window.onload = function(){
   if (ZFDebugLoad) {
      ZFDebugLoad();
   }
   ZFDebugCollapsed();
};

Firebug gives me the error "break on Error too much recursion"

The code to me looks like an endless loop so I'm wondering why the original author put it in. Also there's no return in the function, so ZFDebugLoad would never have a value...

EDIT The actual cause for this error (for any other people that followed the same tutorial I did, the cause to the error was this line

$response->setBody(preg_replace('/(<head.*>)/i', '$1' . $this->_headerOutput(), $response->getBody()));

which uses the regex pattern /(<head.*>)/i, this caused the script to be appended into my HTML5 <header> tag, t correct this I inserted a space into the pattern /<head(?!er).*?>/i


Solution

  • ZFDebugLoad saves the old value of window.onload, and then replaces it with another function.

    On window load it runs the original function first, if it had one, and then runs ZFDebugCollapsed.
    You don't need a return value. In JavaScript, functions are values. if (ZFDebugLoad) check whether ZFDebugLoad is undefined or not, that is, whether you already had a window.onload function before running the script. If it isn't udefined, it can be executed.