Skip to content

Commit

Permalink
fix: logging failing on browsers that don't always have console (#3686)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
misteroneill authored and gkatsev committed Oct 18, 2016
1 parent 44ec0e4 commit e932061
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/js/utils/log.js
Expand Up @@ -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"
Expand All @@ -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) {
Expand All @@ -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);
}
};

Expand Down

0 comments on commit e932061

Please sign in to comment.