Current location: Hot Scripts Forums » General Web Coding » JavaScript » image's onload event behaves strangely


image's onload event behaves strangely

Reply
  #1 (permalink)  
Old 07-07-04, 12:20 PM
davidklonski davidklonski is offline
Newbie Coder
 
Join Date: Apr 2004
Posts: 43
Thanks: 0
Thanked 0 Times in 0 Posts
image's onload event behaves strangely

Hello

I am trying to provide a preview mechinsm for users who will upload images.
This is what my code looks like:
Code:
<img id="image" src="placeholder.gif" width="100" height="100" onload="setImageDimensions(this);"></img>
<input type="file" name="imageLoader" id="imageLoader" onchange="displayPreview(this)">
here is my JavaScript code:
Code:
function displayPreview(fileObj) {
  document.getElementById("image").src = fileObj.value;
}

function setImageDimensions(imageElement) {
   // set the width and height of the new image...
}
The problem is that sometimes the onload event gets fired and sometimes it doesn't. I am not sure why.
The displayPreview function gets called everytime so the onchange event is working correctly.

In times when the onload event is not triggered and thus setImageDimensions is not called, the newly loaded image is displayed using the dimensions of the previous loaded images (or the placeholder if it happens on the first upload) which may cause the image to become distorted.

help is appreciated
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need one MouseOver event to change TWO images AccessGuy JavaScript 5 12-13-03 04:40 AM


All times are GMT -5. The time now is 01:25 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.