diff --git a/tracekit.js b/tracekit.js index 0380b1f..92f1638 100644 --- a/tracekit.js +++ b/tracekit.js @@ -106,6 +106,7 @@ TraceKit.report = (function reportModuleWrapper() { * @param {Function} handler */ function subscribe(handler) { + installGlobalHandler(); handlers.push(handler); } @@ -145,7 +146,7 @@ TraceKit.report = (function reportModuleWrapper() { } } - var _oldOnerrorHandler = window.onerror; + var _oldOnerrorHandler; /** * Ensures all global unhandled exceptions are recorded. @@ -155,7 +156,7 @@ TraceKit.report = (function reportModuleWrapper() { * @param {(number|string)} lineNo The line number at which the error * occurred. */ - window.onerror = function traceKitWindowOnError(message, url, lineNo) { + function traceKitWindowOnError(message, url, lineNo) { var stack = null; if (lastExceptionStack) { @@ -186,7 +187,16 @@ TraceKit.report = (function reportModuleWrapper() { } return false; - }; + } + + function installGlobalHandler () + { + if (window.onerror === traceKitWindowOnError) { + return; + } + _oldOnerrorHandler = window.onerror; + window.onerror = traceKitWindowOnError; + } /** * Reports an unhandled Error to TraceKit.