By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,910 Members | 1,036 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,910 IT Pros & Developers. It's quick & easy.

document.createElement("IMG") vs new Image()

P: 19
Alright, so generally I'm using document.createElement("IMG").... but, I've noticed something just recently that made me switch to "new Image()"

What I'm doing is creating a photo gallery of sorts, where i basically want to supply a total # of images, rows/cols max thumb/full image and let the script do the work. The solution I've opted for is to load the images once and change their size / position / visibility to show "thumb galleries" or "full image size" [and allow for pagination and all the bells & whistles]....

Naturally, the problem relates to the size of the images. When I first load a gallery, I load all the images off screen without height/width specified... I then use the onLoad event of the images to grab the "correct" height/width.

Expand|Select|Wrap|Line Numbers
  1. function Photo( ... ) {
  2. ...
  3. this.element = document.createElement("IMG");
  4. this.element.onload =  callbackFunc( this, "init" );
  5. ...
  6. }
  7.  
  8. /* callbackFunc is a function I use to let me use "this" inside an event handler
  9. -- similar to prototype's "bindAsEventListener" for those familiar with it */
  10.  
  11. /* _event = Event object;  _this = source element (loaded image) */ 
  12. Photo.prototype.init = function( _event, _this )  {
  13.   this.width=_this.width; 
  14.   this.height=_this.height;
  15. }
  16.  
I noticed the height/width of my image were being populated as 0. Only in Internet Explorer -- and intermittently. I think that may have something to do with cached images ... or something. So, I was racking my brain for literally HOURS over this stupid little thing.... and finally, I ended up switching to:

Expand|Select|Wrap|Line Numbers
  1. this.element = new Image()
and now it works in both browsers.... all the time.

anyone else noticed this problem? I saw a post about it on some other forum, but there was no resolution to the problem.
Feb 27 '08 #1
Share this Question
Share on Google+
4 Replies


P: 19
Noooooo IE fails me miserably again.

So, when one is trying to do DOM manipulation between new windows, any elements created must be created in that context.... otherwise IE will raise the most obscure error I have ever seen: "No Such Interface Supported".

Expand|Select|Wrap|Line Numbers
  1. // open up a new window...
  2. var a = window.open("about:blank", "a");
  3.  
  4. // create an element using this document
  5. var test = document.createElement("DIV");
  6. test.appendChild( document.createTextNode("This is a test") );
  7.  
  8. // this works in Firefox; IE returns "no interface supported"
  9. a.document.body.appendChild( test );
  10.  
  11. // the workaround is to change the creation slightly:
  12. var test2 = a.document.createElement("DV");
  13. test2.appendChild( document.createTextNode("test #2") );
  14.  
  15. // this works in both browsers
  16. a.document.body.appendChild( test2 );
  17.  

So that's fine, the way my code is written I can easily enough change my toDOM function to take a parameter of which document to use to create -- and I can easily enough set that to be the document of the new window.... the problem?

Expand|Select|Wrap|Line Numbers
  1. this.element = new Image()
  2.  
Nooooooooooooooooooo..... When I switch it to "createElement" it won't load the images, when I change it to "new Image()" it gives me an "Interface not supported error"............. Is there a way to jiggle the "new Image()" to be created in the context of another document? Anyone?
Feb 27 '08 #2

P: 19
AW hell yea! I got it!

The order of execution I was using:

1) "load Gallery": init the form, loading each image -- which leads to:
2) "image preLoad": create image element & set all properties, including src -- this also sets the "onload" function handler to eventually handle the height/width & finally appends the image to form
4) image loads and the onload handler fires, but with "createElement('IMG')" IE was returning 0/0 for height/width

Basically what I did was move the "appendChild" line from the preLoad code (before it was actually loaded) to the onload handler (after it had finished loading), and that seemed to do the trick. I guess when you append a dynamically-created, non-loaded image to the form, when the image finally does load, while inside the onLoad event handler, the height/width of target will return 0/0... in IE. EVEN THOUGH, and this is the messed up part that had my mind thinking of awful hacks, the height/width are visible in the outerHTML property:

Expand|Select|Wrap|Line Numbers
  1. alert( _this.outerHTML )
  2. /* returned
  3. <IMG
  4.   class=Link
  5.   id=Photo0
  6.   style="LEFT: 0px; POSITION: absolute; TOP: 0px" 
  7.   height=720 src="photos/0.jpg" 
  8.   width=960 border=0>
  9. */
  10. alert( _this.width );
  11. // returned 0
  12.  
I've said it before and I'll say it again: IE can suck my nuts.
Feb 27 '08 #3

P: 19
Ah ha ha ha.... I spoke too soon.

IE displays the images fine, which was the original problem... the first "onclick" that I call (clicking an image to show full size) works, but raising any event handlers after that will .. get this .. completely hang the IE window..... no errors, no "infinite loop cancel script" prompt... nothing. Just [Not Responding].

I'll post more when I figure it out... my hunch is it has something to do with re-attaching the events after it loads -- but the strange thing is, the first event loads fine..... I'm using the callbackFunc for each.

IE memory leak problems, anyone?

[edit] Oh yea... need I mention that Firefox works fine?
Feb 28 '08 #4

acoder
Expert Mod 15k+
P: 16,027
IE displays the images fine, which was the original problem... the first "onclick" that I call (clicking an image to show full size) works, but raising any event handlers after that will .. get this .. completely hang the IE window..... no errors, no "infinite loop cancel script" prompt... nothing. Just [Not Responding].

I'll post more when I figure it out... my hunch is it has something to do with re-attaching the events after it loads -- but the strange thing is, the first event loads fine..... I'm using the callbackFunc for each.

IE memory leak problems, anyone?
Possibly. Can you post an example.

Interesting problem. Thanks for posting your 'discoveries' :)
Feb 28 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.