473,783 Members | 2,269 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

document.create Element("IMG") vs new Image()

19 New Member
Alright, so generally I'm using document.create Element("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 23926
tswaters
19 New Member
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.  
Noooooooooooooo ooooo..... When I switch it to "createElem ent" 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
tswaters
19 New Member
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 "appendChil d" 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
tswaters
19 New Member
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 Recognized Expert Moderator MVP
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
13087
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 - ImageName - for processing, or the return data which is done (or not as the case may be) by imagejpeg($img) in the script, after header("Content-type: image/jpeg"). Any insights would be most welcome, especially debugging techniques and a pointer to...
6
15057
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 target inside the javascript function, I get very very little love from javascript in an XHTML setting.... I have put up the file that demonstrates my problem at: http://www.nihongoresources.com/downloads/test/test.xml (the jscript bit is...
1
4819
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 command in some type of array. I added the mailbox in the lower left corner (see link below)http://www.aamechanical.com/indextemp.htm but I cannot get it to swap. Here is the code for the page: Thanks for your help in advance
15
2851
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 to mean in this context. Can someone explain? TIA
9
1901
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" alt="Click" /> </a> WebResource3.gif is an arrow pointing left and when I click on it I
8
4340
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 see without scrolling down the div the tooltip is shown at the desired place but when you scroll donw the div the tooltip's position gets all messed up, e.g in the last "ΤΙΤΛΟΣ ALBUM" (which means Title of album ) the tooltip gets in front of the...
5
3086
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 == "red.jpg") {
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8968
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6735
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.