[二]烦透的动态file上传

本文介绍了一种在Firefox浏览器中解决iframe内元素加载延迟的方法,通过使用setTimeout实现延迟创建DOM对象,确保了跨浏览器的一致性。

 

<html>
<head>
<script type="text/javascript">

    show_on 
= function(){
        
var myinput = document.getElementById("myinput");
        
        
var ifrm = document.createElement("iframe");
        ifrm.setAttribute(
"style","width:283px;height:230px;");
        ifrm.setAttribute(
"scrolling","no");
        ifrm.setAttribute(
"frameborder","1");
        myinput.appendChild(ifrm);
        ifrm 
= ifrm.contentWindow.document;
        
        
var ifrm_body = ifrm.body;
        
        
var ofile = ifrm.createElement("input");
        ifrm_body.appendChild(ofile);

    }


window.onload 
= show_on;
</script>
</head>
<body>
<span id="myinput"></span>
</body>
</html>

动态file上传需要马上创建一个iframe,然后在iframe中添加一个input,在IE中,很轻松就实现了.

轻松在IE中添加,但问题很快就来了..在FF中完全没反应..怎么回事?

原来FF中控制iframe必须用ifm.contentWindow.document.body来添加节点,因为它对空的页面默认添加body节点.所以修改为如下:

<html>
<head>
<script type="text/javascript">

    show_on 
= function(){
        
var myinput = document.getElementById("myinput");
        
        
var ifrm = document.createElement("iframe");
        ifrm.setAttribute(
"style","width:283px;height:230px;");
        ifrm.setAttribute(
"scrolling","no");
        ifrm.setAttribute(
"frameborder","1");
        myinput.appendChild(ifrm);
        ifrm 
= ifrm.contentWindow.document;
        
        
var ifrm_body = ifrm.body;
        
        
var ofile = ifrm.createElement("input");
        ifrm_body.appendChild(ofile);

    }


window.onload 
= show_on;
</script>
</head>
<body>
<span id="myinput"></span>
</body>
</html>

本来以为成功了,谁知道打开,还是什么都没有..怎么回事?用alert(ifrm_body.innerHTML);发现添加完全正常,发贴到优快云..有网友说是"消失的生命"..真贴切..

结果在第二天.,某位高人告诉我是FF延迟,通过setTimeout,在其中重新创建对象,加上延迟,结果创建成功了..

<html>
<head>
<script type="text/javascript">

    show_on 
= function(){
         myinput 
= document.getElementById("myinput");
        ifrm 
= document.createElement("iframe");
        ifrm.setAttribute(
"style","width:283px;height:230px;");
        ifrm.setAttribute(
"scrolling","no");
        myinput.appendChild(ifrm);
        ifrm_d 
= ifrm.contentWindow.document;
        
var ifrm_body = ifrm_d.body;
        
var ofile = ifrm_d.createElement("input");
        ofile.value
="asdf";
        ifrm_body.appendChild(ofile);
        
        setTimeout(tt,
"20ms");

    }

    
function tt(){
        ifrm_d 
= ifrm.contentWindow.document;
      
        
var ifrm_body = ifrm_d.body;
        
var ofile = ifrm_d.createElement("input");
        ifrm_body.appendChild(ofile);
}


window.onload 
= show_on;
</script>
</head>
<body>
<div id="myinput"></span>
<input type="button" id="sd" onClick="tt()">
</body>
</html>

神奇在于,把SetTimeout(tt(),0)同样正常运行..

这让我想起了在AJAX中,FF的BUG..

通过SetTimeout( fun,0)能解决许多问题..

下次如果再次出现"消失的生命",可以尝试用SetTimeout方法来解决..

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值