473,406 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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
4 23806
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
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
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
16,027 Expert Mod 8TB
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

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

Similar topics

14
by: Gregory | last post by:
Hello, I'm trying to do the above in order to process an image and return the result to an html image control. It fails and my key suspects are either the variable that I'm passing in -...
6
by: Mike Kamermans | last post by:
I'm trying to determine where inside an XHTML's DOM a javascript function is triggered, but when I use the construction of calling a function with "this" as parameter and then checking the event...
1
by: Allen | last post by:
I am trying to add an additional photo/hyperlink to the company web site (I didn't create it) without any luck. The mouseover feature 'highlights' pics by swapping them with another pic using this...
15
by: Sander Tekelenburg | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 The HTML specs speak of "replaced" and "non-replaced" elements, yet for the life of me I can't find an explanation of what "replaced" is supposed...
9
by: Burak Gunay | last post by:
Hello, In my asp.net 2.0 page, I have a hyperlink with an arrow image <a id="imgtest" href="#"onclick="return hideColumn()"> <img id="imgArrow" src="App_Themes/Trmis/WebResource3.gif"...
8
mikek12004
by: mikek12004 | last post by:
1) Script Title: Rich HTML Balloon Tooltip 2) Script URL :http://www.dynamicdrive.com/dynamicindex5/balloontooltip.htm 3) Problem Description: See this page General Music for links you can...
5
by: enaz | last post by:
Ok, so, this is my first post, if there is any info i am leaving out please tell me this is the function: function mouseOver(c) { alert(c); <!-- just for debugging--> if (c.src ==...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.