不能console

IE8下 浏览器不打开控制台使用console.log 是报错的 。

解决方法

<script type="text/javascript">

  //解决 IE8、IE9 不支持 console 问题

  window.console = window.console || (function () {

    var c = {}; c.log = c.warn = c.debug = c.info = c.error = c.time = c.dir = c.profile

    = c.clear = c.exception = c.trace = c.assert = function () { };

    return c;

  })();

</script>

这样可以使console不报错。但是会影响功能,可以使用下面方法

window._console =window.console;//将原始console对象缓存

window.console = (function(orgConsole){

return{//构造的新console对象

log: getConsoleFn("log"),

debug: getConsoleFn("debug"),

info: getConsoleFn("info"),

warn: getConsoleFn("warn"),

exception: getConsoleFn("exception"),

assert: getConsoleFn("assert"),

dir: getConsoleFn("dir"),

dirxml: getConsoleFn("dirxml"),

trace: getConsoleFn("trace"),

group: getConsoleFn("group"),

groupCollapsed: getConsoleFn("groupCollapsed"),

groupEnd: getConsoleFn("groupEnd"),

profile: getConsoleFn("profile"),

profileEnd: getConsoleFn("profileEnd"),

count: getConsoleFn("count"),

clear: getConsoleFn("clear"),

time: getConsoleFn("time"),

timeEnd: getConsoleFn("timeEnd"),

timeStamp: getConsoleFn("timeStamp"),

table: getConsoleFn("table"),

error: getConsoleFn("error"),

memory: getConsoleFn("memory"),

markTimeline: getConsoleFn("markTimeline"),

timeline: getConsoleFn("timeline"),

timelineEnd: getConsoleFn("timelineEnd")

    };

functiongetConsoleFn(name){

returnfunctionactionConsole(){

if(typeof(orgConsole) !=="object")return;

if(typeof(orgConsole[name]) !=="function")return;//判断原始console对象中是否含有此方法,若没有则直接返回

returnorgConsole[name].apply(orgConsole,Array.prototype.slice.call(arguments));//调用原始函数

        };

    }

}(window._console));


参考https://blog.csdn.net/qq_34986769/article/details/52161785

https://www.cnblogs.com/wz122889488/p/6272751.html

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容