ITeye博客

JS加载问题.

java_frog · 2009-10-12 21:58 · Markdown

if(document.getElementById("mytask2").readyState=="complete"){
	window.attachEvent('onload',function(){
			if(result == 0){
				 document.getElementById("mytask2").innerHTML="&nbsp;&nbsp;&nbsp;&nbsp;<a href='http://cmri.hq.cmcc/pc' target='_blank' style='cursor:hand'>您没有未落实的任务</a>";	
			}else{
				document.getElementById("mytask2").innerHTML="&nbsp;&nbsp;&nbsp;&nbsp;<a href='http://cmri.hq.cmcc/ActionSpace' target='_blank' style='cursor:hand'>ActionSpace待办事项: "+result+"项</a>";
			}
							})
}

在HTML结构代码还没有加载完是,JS代码先加载运行了, 就会产生这个"Internet Explorer 无法打开站点"错误. 在IE下,当页面还没有加载完全时,如果正在执行的JS代码中含有使用了document.createElement、appendChild或者innerHTML操作的话,很容易引起页面加载失败。导 致提示"internet explore 无法打开internet站点 http://www.xxx.com/xxx.html 已终止操作"。

这是因为在IE下,在加载文档的过程中,整个HTML文档的DOM结构尚未生成完整,而此时正在执行的JS就已创建出新的DOM结点了,致使DOM树的结构发生紊乱。

解决方法如下:

1、加载完成判断 if (document.readyState=="complete"){ .... }else{ document.onreadystatechange=function(){ if(document.readyState=="complete")...; } 2、加载完后执行 window.onload=function(){ ... } 3. dom结构加载完 window.addEvent('domready',function(){ ............. })

在appendChild或者innerHTML操作处判断document.readyState=="complete", 若为否,则setTimeout若干秒之后重新作这个操作。

但是,此属性只对ie,opeara有效,ff的document没有readyState属性,永远是undefined.

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。