Home  >  Article  >  Web Front-end  >  Brief solution sharing for browser compatible console objects_javascript skills

Brief solution sharing for browser compatible console objects_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:19:001150browse

The browser reports that the console object cannot be found, so we manually construct a console object with the same interface and place it in the window. Empty methods and empty objects are used here. In this way, even in very old browsers, the code containing console.xxxxx will still not report errors and run perfectly.

Attached below is the repair compatibility code, which must be placed before the first console.xxxx call, otherwise it will be meaningless.

Copy code The code is as follows:

(function (){

/ /Create an empty console object to avoid JS errors

if(!window.console)
window.console = {};
var console = window.console;

var funcs = ['assert', 'clear', 'count', 'debug', 'dir', 'dirxml',
'error', 'exception', 'group', 'groupCollapsed', 'groupEnd',
           'info', 'log', 'markTimeline', 'profile', 'profileEnd', 
                                                                                                                                       'info', 'log', 'markTimeline', 'profile', 'profileEnd', 
                 'table', 'time', 'timeEnd', 'timeStamp', 'trace', 'warn'];
for(var i=0,l=funcs.length;i var func = funcs[i];
if(!console[func])
console[ func] = function(){};
}
if(!console.memory)
console.memory = {};

})();
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn