美文网首页
不能console

不能console

作者: 海子小夜曲 | 来源:发表于2019-01-09 15:54 被阅读0次

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

相关文章

网友评论

      本文标题:不能console

      本文链接:https://www.haomeiwen.com/subject/xcdgrqtx.html