首页 新闻 会员 周边 捐助

IE6789浏览器有没有console.log类似的方法可以输出调试内容但又不影响页面正常运行的JS代码呢?

0
悬赏园豆:5 [已解决问题] 解决于 2012-04-05 11:54

请问IE6789浏览器有没有console.log类似的方法可以输出调试内容但又不影响页面正常运行的JS代码呢?

观海云不远的主页 观海云不远 | 初学一级 | 园豆:89
提问于:2012-03-26 09:07
< >
分享
最佳答案
0

1:Complete cross-browser console.log(),兼容几乎所有浏览器的代码

/ Tell IE9 to use its built-in console

if (Function.prototype.bind && console && typeof console.log == "object") {

["log","info","warn","error","assert","dir","clear","profile","profileEnd"]

.forEach(function (method) {

console[method] = this.call(console[method], console);

}, Function.prototype.bind);

}


// log() -- The complete, cross-browser (we don't judge!) console.log wrapper for his or her logging pleasure

if (!window.log) {

window.log = function () {

log.history = log.history || []; // store logs to an array for reference

log.history.push(arguments);

// Modern browsers

if (typeof console != 'undefined' && typeof console.log == 'function') {


// Opera 11

if (window.opera) {

var i = 0;

while (i < arguments.length) {

console.log("Item " + (i+1) + ": " + arguments[i]);

i++;

}

}


// All other modern browsers

else if ((Array.prototype.slice.call(arguments)).length == 1 && typeof Array.prototype.slice.call(arguments)[0] == 'string') {

console.log( (Array.prototype.slice.call(arguments)).toString() );

}

else {

console.log( Array.prototype.slice.call(arguments) );

}


}


// IE8

else if (!Function.prototype.bind && typeof console != 'undefined' && typeof console.log == 'object') {

Function.prototype.call.call(console.log, console, Array.prototype.slice.call(arguments));

}


// IE7 and lower, and other old browsers

else {

// Inject Firebug lite

if (!document.getElementById('firebug-lite')) {

// Include the script

var script = document.createElement('script');

script.type = "text/javascript";

script.id = 'firebug-lite';

// If you run the script locally, point to /path/to/firebug-lite/build/firebug-lite.js

script.src = 'https://getfirebug.com/firebug-lite.js';

// If you want to expand the console window by default, uncomment this line

//document.getElementsByTagName('HTML')[0].setAttribute('debug','true');

document.getElementsByTagName('HEAD')[0].appendChild(script);

setTimeout(function () { log( Array.prototype.slice.call(arguments) ); }, 2000);

}

else {

// FBL was included but it hasn't finished loading yet, so try again momentarily

setTimeout(function () { log( Array.prototype.slice.call(arguments) ); }, 500);

}

}

}

}

2:专门为ie下调试js用,Faux Console,使用方法

<style type="text/css">@import 'fauxconsole.css';</style>

<script type="text/javascript" src="fauxconsole.js"></script>

console.log(message) - adds message to the log
console.show() - shows the console
console.hide() - hides the console

注:非原创,答案来自德问


收获园豆:5
zsounder | 老鸟四级 |园豆:2819 | 2012-03-26 09:17
其他回答(2)
0
dudu | 园豆:30778 (高人七级) | 2012-03-26 10:20
0

流萤赴会 | 园豆:202 (菜鸟二级) | 2015-02-24 22:16
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册