Connecting Tech Pros Worldwide Forums | Help | Site Map

DOM methods showing different results in IE

Familiar Sight
 
Join Date: Sep 2006
Location: Paris, France
Posts: 143
#1: Mar 9 '07
Hi...

I'm "ajaxifying" my image gallery and I have some code which creates the elements on the fly and places them on the page.

Using firefox, the code works fine and the elements show up as they should, however in IE, the UL and LI's appear to have no style associated with them at all and don't display as they should.

They are styled with CSS and it's the same layout as the non ajax version, which works fine in IE.

The way the html should look after javascript:

[HTML]<h1>Photo album: France, July 2006</h1>
<span id="page_count">Page 1 of 5</span>
<ul id="gallery">

<li class="imgcontainer" title="Yaron and yup, that's the Catheral Notre Dame de Paris... again.">
<div>
<a href="/simpleTEST/www/?pid=2&amp;a=44&amp;im=1">
<img src="data/thumbs/album_44_image_306.jpg" alt="" width="80" height="60" />

</a>
</div>
</li>

<li class="imgcontainer" title="">
<div>
<a href="/simpleTEST/www/?pid=2&amp;a=44&amp;im=2">
<img src="data/thumbs/album_44_image_307.jpg" alt="" width="80" height="60" />
</a>
</div>

</li>

<li class="imgcontainer" title="">
<div>
<a href="/simpleTEST/www/?pid=2&amp;a=44&amp;im=3">
<img src="data/thumbs/album_44_image_308.jpg" alt="" width="60" height="80" />
</a>
</div>
</li>

<li class="imgcontainer" title="">

<div>
<a href="/simpleTEST/www/?pid=2&amp;a=44&amp;im=4">
<img src="data/thumbs/album_44_image_309.jpg" alt="" width="60" height="80" />
</a>
</div>
</li>

<li class="imgcontainer" title="">
<div>
<a href="/simpleTEST/www/?pid=2&amp;a=44&amp;im=5">

<img src="data/thumbs/album_44_image_310.jpg" alt="" width="80" height="60" />
</a>
</div>
</li>

<li class="imgcontainer" title="">
<div>
<a href="/simpleTEST/www/?pid=2&amp;a=44&amp;im=6">
<img src="data/thumbs/album_44_image_311.jpg" alt="" width="60" height="80" />
</a>

</div>
</li>

<li class="imgcontainer" title="Yaron wanted all these pics... seriously.">
<div>
<a href="/simpleTEST/www/?pid=2&amp;a=44&amp;im=7">
<img src="data/thumbs/album_44_image_312.jpg" alt="" width="60" height="80" />
</a>
</div>
</li>


<li class="imgcontainer" title="">
<div>
<a href="/simpleTEST/www/?pid=2&amp;a=44&amp;im=8">
<img src="data/thumbs/album_44_image_313.jpg" alt="" width="80" height="60" />
</a>
</div>
</li>

</ul>
<p id="img_desc">

I have a couple of great friends... Mike and Yaron drove over to France with me to help shift my stuff when I moved here.
</p>[/HTML]

The javascript which creates this html:

Expand|Select|Wrap|Line Numbers
  1.       if (galleryObj) {
  2.         clearInterval(timer);
  3.  
  4.         galleryTitle = dom.newElement(d, "h1");
  5.         galleryTitle.appendChild(dom.addText(d, "Photo album: " + galleryObj.gallery.title));
  6.  
  7.         galleryPage  = dom.newElement(d, "span");
  8.         galleryPage.setAttribute("id", "page_count");
  9.  
  10.         thumbnailUl  = dom.newElement(d, "ul");
  11.         thumbnailUl.setAttribute("id", "gallery");
  12.  
  13.         galleryDesc  = dom.newElement(d, "p");
  14.         galleryDesc.setAttribute("id", "img_desc");
  15.         galleryDesc.appendChild(dom.addText(d, galleryObj.gallery.desc));
  16.  
  17.         for (var i = 0; i < galleryObj.gallery.thumbs_per_page; i++) {
  18.           if (image = galleryObj.gallery.image[i]) {
  19.             thumbnailLi  = dom.newElement(d, "li");
  20.             thumbnailLi.setAttribute("class", "imgcontainer");
  21.             thumbnailDiv = dom.newElement(d, "div");
  22.             thumbnailA   = dom.newElement(d, "a");
  23.             thumbnailImg = dom.newElement(d, "img");
  24.             thumbnailA.setAttribute("href", "javascript: simple.enlarge(" + image.img_id + ")");
  25.             thumbnailImg.setAttribute("src", image.thumb);
  26.             thumbnailImg.setAttribute("alt", image.desc);
  27.             // append elements
  28.             thumbnailA.appendChild(thumbnailImg);
  29.             thumbnailDiv.appendChild(thumbnailA);
  30.             thumbnailLi.appendChild(thumbnailDiv);
  31.             thumbnailUl.appendChild(thumbnailLi);
  32.           }
  33.         }
  34.  
  35.         // display after a delay
  36.         setTimeout(function() {
  37.           d.getElementById('content').appendChild(galleryTitle);
  38.           d.getElementById('content').appendChild(galleryPage);
  39.           d.getElementById('content').appendChild(thumbnailUl);
  40.           d.getElementById('content').appendChild(galleryDesc);
  41.           // after work is done, hide loadingAnim and show thumbs
  42.           loadingAnim.style.display = "none";
  43.         }, 1000);
  44.      }

As you can see, I'm using DOM methods to create and add the elements to my HTML. Both IE6 and 7 appear to show the unordered list containing the thumbs with no style. The rest of the elements are styled as they should be.

Any ideas appreciated.

Thanks

Needs Regular Fix
 
Join Date: Jun 2006
Posts: 424
#2: Mar 9 '07

re: DOM methods showing different results in IE


your line
Quote:
thumbnailLi.setAttribute("class", "imgcontainer");
is fine for most browsers, but not ie.

To work in all browsers set the class as a property, not an attribute:
thumbnailLi.className="imgcontainer";
Familiar Sight
 
Join Date: Sep 2006
Location: Paris, France
Posts: 143
#3: Mar 9 '07

re: DOM methods showing different results in IE


Quote:

Originally Posted by mrhoo

your line

is fine for most browsers, but not ie.

To work in all browsers set the class as a property, not an attribute:
thumbnailLi.className="imgcontainer";

Wow that's nuts, but it works! Thanks... I suppose this is only for classes, seeing as how the other elements using "id" work fine?

Excellent... oh, one more question.

I wrote a small simple tooltip class and I assign an event to <a> tags on the page. I use the standards method of obj.addEventListener(event, func, false); and obj.attachEvent('on'+event, func); for W3C browsers and IE, respectively - however, the event only passes the object to the function call in good browsers. In IE, the function that's called has no access to the object. Instead, I have to use the .onmouseover = funcName;

Any idea why?

Thanks
Familiar Sight
 
Join Date: Sep 2006
Location: Paris, France
Posts: 143
#4: Mar 9 '07

re: DOM methods showing different results in IE


Ok, I figured I'd just post an answer to my second question:

from http://www.quirksmode.org/js/events_advanced.html

Quote:
When compared to the W3C model, the Microsoft model has two important drawbacks:

1. Events always bubble, no capturing possibility.
2. The event handling function is referenced, not copied, so the this keyword always refers to the window and is completely useless.
Sigh
Reply


Similar JavaScript / Ajax / DHTML bytes