IE8 console未定义

本文介绍了一种解决ExtJS在IE8浏览器中console未定义的问题的方法,并提供了一个具体的例子,通过添加模拟console对象的方式使得console能在IE8中正常工作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

写了一个extjs页面,在chrome下调试好好的,在ie8下调试页面打开,进度条没按预期更新。代码如下:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>ExtJs</title>
	<meta charset="GBK">
	<link href="ExtJs/resources/css/ext-all.css" rel="stylesheet" type="text/css" />
	<script type="text/javascript" src="ExtJs/ext-base.js"></script>
	<script type="text/javascript" src="ExtJs/ext-all-debug-w-comments.js"></script> 

</head>
<body>
    <div>
    <script type="text/javascript">

	Ext.MessageBox.show({
           title: '倒计时提醒',
       <span style="white-space:pre">	</span>   msg: '这是一个倒计时',
           buttons:{"ok":"知道了"},
           animEl: 'mb9',
	   closable : false,
	   width : 300,
	   progress : true,
	   progressText: '5秒后将xxx',
           fn: answerPhone
	});
	
	var timeLeft = 5;
        var countDownTask = {
		run:countDownFunc,
		interval:1000
	};
        function countDownFunc(){
	    console.log("fuck" + timeLeft);
	    if(timeLeft <= 0){
		    Ext.MessageBox.updateProgress(1, "倒计时结束");
			Ext.MessageBox.buttons = false;
			//简单延迟后关闭窗口
			setTimeout(function(){Ext.MessageBox.hide()}, 500);
			//终止轮询任务
			Ext.TaskMgr.stop(countDownTask);
			answerPhone();
		}
		else{
		    timeLeft--;
		    Ext.MessageBox.updateProgress((5-timeLeft)/5, timeLeft+'秒之后将xxxx');
		}
	}
	Ext.TaskMgr.start(countDownTask);
	
	var answerPhone = function(){
	   console.log("倒计时后调用的函数。");
	};
	
    </script>
    </div>
</body>
</html>


按F12查看控制台信息,发现这么一句"console未定义"。网上查了一下,有两种解决办法


1.注释掉console的语句(好暴力....)

2.但是有时候实在是需要支持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; 
    })();


加入上述片段后,ie8就可以使用console了,修改后的代码

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>ExtJs</title>
	<meta charset="GBK">
	<link href="ExtJs/resources/css/ext-all.css" rel="stylesheet" type="text/css" />
	<script type="text/javascript" src="ExtJs/ext-base.js"></script>
	<script type="text/javascript" src="ExtJs/ext-all-debug-w-comments.js"></script> 

</head>
<body>
    <div>
    <script type="text/javascript">
	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; 
        })();
	
	Ext.MessageBox.show({
           title: '倒计时提醒',
           msg: '这是一个倒计时',
           buttons:{"ok":"知道了"},
           animEl: 'mb9',
	   closable : false,
	   width : 300,
	   progress : true,
	   progressText: '5秒后将xxx',
           fn: answerPhone
	});
	
	var timeLeft = 5;
    var countDownTask = {
		run:countDownFunc,
		interval:1000
	};
    function countDownFunc(){
	    console.log("fuck" + timeLeft);
	    if(timeLeft <= 0){
		    Ext.MessageBox.updateProgress(1, "倒计时结束");
			Ext.MessageBox.buttons = false;
			//简单延迟后关闭窗口
			setTimeout(function(){Ext.MessageBox.hide()}, 500);
			//终止轮询任务
			Ext.TaskMgr.stop(countDownTask);
			answerPhone();
		}
		else{
		    timeLeft--;
		    Ext.MessageBox.updateProgress((5-timeLeft)/5, timeLeft+'秒之后将xxxx');
		}
	}
	Ext.TaskMgr.start(countDownTask);
	
	var answerPhone = function(){
	   console.log("倒计时后调用的函数。");
	};
	
    </script>
    </div>
</body>
</html>






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值