JS图片预览,兼容FF和IE
<script> var picPath; var image; // preview picture function preview() { document.getElementById('preview').style.display = 'none'; // 下面代码用来获得图片尺寸,这样才能在IE下正常显示图片 document.getElementById('box').innerHTML = "<img width='"+image.width+"' height='"+image.height+"' id='aPic' src='"+picPath+"'>"; } // show view button function buttonShow() { if ( image.width == 0 || image.height == 0 ) { setTimeout(buttonShow, 1000); } else { document.getElementById('preview').style.display = 'block'; } } function loadImage(ele) { picPath = getPath(ele); image = new Image(); image.src = picPath; setTimeout(buttonShow, 1000); } function getPath(obj) { if(obj) { //ie if (window.navigator.userAgent.indexOf("MSIE")>=1) { obj.select(); // IE下取得图片的本地路径 return document.selection.createRange().text; } //firefox else if(window.navigator.userAgent.indexOf("Firefox")>=1) { if(obj.files) { // Firefox下取得的是图片的数据 return obj.files.item(0).getAsDataURL(); } return obj.value; } return obj.value; } } </script> </head> <body> <input type="file" name="pic" id="pic" onchange='loadImage(this)' /> <input id='preview' type='button' value='preview' style='display:none;' onclick='preview();'> <div id='box'></div>