From e932061024f9222cb712ed0495a21ac06e3a33d0 Mon Sep 17 00:00:00 2001 From: Pat O'Neill Date: Tue, 18 Oct 2016 12:02:28 -0400 Subject: [PATCH] fix: logging failing on browsers that don't always have console (#3686) In browsers like IE9, `console` isn't always available. This, we need to apply `console` to our console functions but bail out early if `window.console` isn't defined. --- src/js/utils/log.js | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/js/utils/log.js b/src/js/utils/log.js index fca15985b2..85e5f7b575 100644 --- a/src/js/utils/log.js +++ b/src/js/utils/log.js @@ -19,14 +19,6 @@ let log; */ export const logByType = (type, args, stringify = !!IE_VERSION && IE_VERSION < 11) => { - // If there's no console then don't try to output messages, but they will - // still be stored in `log.history`. - // - // Was setting these once outside of this function, but containing them - // in the function makes it easier to test cases where console doesn't exist - // when the module is executed. - const fn = window.console && window.console[type] || function() {}; - if (type !== 'log') { // add the type to the front of the message when it's not "log" @@ -39,6 +31,19 @@ export const logByType = (type, args, stringify = !!IE_VERSION && IE_VERSION < 1 // add console prefix after adding to history args.unshift('VIDEOJS:'); + // If there's no console then don't try to output messages, but they will + // still be stored in `log.history`. + // + // Was setting these once outside of this function, but containing them + // in the function makes it easier to test cases where console doesn't exist + // when the module is executed. + const fn = window.console && window.console[type]; + + // Bail out if there's no console. + if (!fn) { + return; + } + // IEs previous to 11 log objects uselessly as "[object Object]"; so, JSONify // objects and arrays for those less-capable browsers. if (stringify) { @@ -62,7 +67,7 @@ export const logByType = (type, args, stringify = !!IE_VERSION && IE_VERSION < 1 if (!fn.apply) { fn(args); } else { - fn[Array.isArray(args) ? 'apply' : 'call'](console, args); + fn[Array.isArray(args) ? 'apply' : 'call'](window.console, args); } };