Skip to content

Instantly share code, notes, and snippets.

@UserAd
Created August 21, 2010 11:50
Show Gist options
  • Save UserAd/542199 to your computer and use it in GitHub Desktop.
Save UserAd/542199 to your computer and use it in GitHub Desktop.
var _ImagePreview = function() {
this.image = null;
this.image_object = new Image();
this.image_view = function(el,dest)
{
var imagefile = document.getElementById(el);
this.image = document.getElementById(dest);
// HTML5 FileAPI: Firefox 3.6+, Chrome 6+
if(typeof(FileReader)!='undefined')
{
var reader = new FileReader();
var self = this;
reader.onload = function(e){
self.image.src = e.target.result;
}
reader.readAsDataURL(imagefile.files.item(0));
}
// Firefox 3.0-3.6
else if(imagefile.files && imagefile.files.item(0).getAsDataURL)
{
this.image.src = imagefile.files.item(0).getAsDataURL();
}
// Safari
else if(imagefile.files && !imagefile.files.item(0).getAsDataURL)
{
//Safari and webkit not supported now.
}
//IE!!!!
else
{
try
{
this.image_object.src = imagefile.value;
var self = this;
setTimeout(function(){
self.image.src = self.image_object.src;
},200);
}
catch(e){}
}
}
}
var ImagePreview = new _ImagePreview();
@TALlama
Copy link

TALlama commented Aug 24, 2010

I wrote a slightly more generalized version that'll push you a URL whenever one changes, so you can use it for more cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment