`
xixian
  • 浏览: 215377 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

js 兼容ie6,ie7,firefox图片预览

    博客分类:
  • js
阅读更多
<!doctype html public "-//w3c//dtd html 4.01//en" "http://www.w3.org/tr/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>New file input tester.</title>
</head>
<body>
<input id="myfile" type="file" />
<br />
<img src="" alt="Image to be upload." width="50px" height="50px"/>
<img id="ss" src="" alt="Image to be upload." width="400px" height="400px"/>
<div id="info"></div>
<script type="text/javascript">
var dFile = document.getElementById('myfile');
var dImg = document.getElementsByTagName('img')[0];
var dInfo = document.getElementById('info');
dFile.onchange = function(){
 if(!dFile.value.match(/.jpg|.gif|.png|.bmp/i)){alert('File type must be: .jpg, .gif, .bmp or .png !');return;}
 if(dFile.files){
 document.getElementById("ss").src=dFile.files[0].getAsDataURL();
  dImg.src = dFile.files[0].getAsDataURL();
 }else if(dFile.value.indexOf('\\') > -1 || dFile.value.indexOf('\/') > -1){
  dImg.src = dFile.value;
   document.getElementById("ss").src=dFile.value;
 }
}
</script>
<p>判断远程图片是否存在,不存在的话替换成默认的图片,代码如下:</p>
<p>第一种情况存在http://www.iteye.com/images/logo-small.gif</p>
<IMG src="http://www.iteye.com/images/logo-small.gif" onerror="javascript:this.src='http://www.iteye.com/upload/logo/user/233547/cd53fca0-a8a7-3fe5-b4c9-c2c6c3d2fc4a.jpg?1271142474'">
<p>第二种情况不存在http://www.iteye.com/images/logo-small11.gif,<br>替换成本人的logo^_^</p>
<IMG src="http://www.iteye.com/images/logo-small11.gif" onerror="javascript:this.src='http://www.iteye.com/upload/logo/user/233547/cd53fca0-a8a7-3fe5-b4c9-c2c6c3d2fc4a.jpg?1271142474'">

</body>
</html>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics