这个主要是在做Ajax无刷新上传的时候用了。
其实也可以直接写Iframe在Html上,但考虑到会不简洁,怕误删等其他操作造成错误,就用这个动态创建的。
狗狗了一下找到下面一段代码:
var objBody = document.getElementsByTagName("body").item(0);
var iframe = document.createElement('iframe');
iframe.id = 'fileUploaderEmptyHole';
iframe.name = 'fileUploaderEmptyHole';
iframe.width = 0;
iframe.height = 0;
iframe.marginHeight = 0;
iframe.marginWidth = 0;
objBody.insertBefore(iframe, objBody.firstChild);
这段代码在Firefox运行正常,但在Ie下不行。会在新弹出的窗口中打开。
调试了一下,发现在IE里是iframe.name = 'fileUploaderEmptyHole'; 这行代码无效。
再次更改代码,使用
iframe = document.createElement('<iframe name="fileUploaderEmptyHole">');
这样的形式。但是这样子在非IE的浏览器里会抛出异常。
所以。。 那么。。 捕获异常判断浏览器
最后再处理的兼容Firefox/IE等浏览器的动态创建一个Iframe的Js代码 如下:
var iframe;
try {
iframe = document.createElement('<iframe name="fileUploaderEmptyHole">');
} catch (ex) {
iframe = document.createElement('iframe');
}
iframe.id = 'fileUploaderEmptyHole';
iframe.name = 'fileUploaderEmptyHole';
iframe.width = 0;
iframe.height = 0;
iframe.marginHeight = 0;
iframe.marginWidth = 0;
作者: Sjolzy
--EOF--
学习了。这个js可以用吗?能兼容chrome浏览器吗?
评分:5分
万家购物 : 2012-05-18 08:44
学习了。这个js可以用吗?能兼容chrome浏览器吗?