Created
August 21, 2010 11:50
-
-
Save UserAd/542199 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I wrote a slightly more generalized version that'll push you a URL whenever one changes, so you can use it for more cases.