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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tswaters
    New Member
    • Feb 2007
    • 19

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

    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.

    [code=javascript]
    function Photo( ... ) {
    ...
    this.element = document.create Element("IMG");
    this.element.on load = callbackFunc( this, "init" );
    ...
    }

    /* callbackFunc is a function I use to let me use "this" inside an event handler
    -- similar to prototype's "bindAsEventLis tener" for those familiar with it */

    /* _event = Event object; _this = source element (loaded image) */
    Photo.prototype .init = function( _event, _this ) {
    this.width=_thi s.width;
    this.height=_th is.height;
    }
    [/code]

    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:

    [code=javascript]this.element = new Image()[/code]

    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.
  • tswaters
    New Member
    • Feb 2007
    • 19

    #2
    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".

    [code=javascript]
    // open up a new window...
    var a = window.open("ab out:blank", "a");

    // create an element using this document
    var test = document.create Element("DIV");
    test.appendChil d( document.create TextNode("This is a test") );

    // this works in Firefox; IE returns "no interface supported"
    a.document.body .appendChild( test );

    // the workaround is to change the creation slightly:
    var test2 = a.document.crea teElement("DV") ;
    test2.appendChi ld( document.create TextNode("test #2") );

    // this works in both browsers
    a.document.body .appendChild( test2 );
    [/code]


    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?

    [code=javascript]
    this.element = new Image()
    [/code]

    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?

    Comment

    • tswaters
      New Member
      • Feb 2007
      • 19

      #3
      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:

      [code=javascript]
      alert( _this.outerHTML )
      /* returned
      <IMG
      class=Link
      id=Photo0
      style="LEFT: 0px; POSITION: absolute; TOP: 0px"
      height=720 src="photos/0.jpg"
      width=960 border=0>
      */
      alert( _this.width );
      // returned 0
      [/code]

      I've said it before and I'll say it again: IE can suck my nuts.

      Comment

      • tswaters
        New Member
        • Feb 2007
        • 19

        #4
        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?

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Originally posted by tswaters
          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' :)

          Comment

          Working...