Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #7497 (closed defect: fixed)

Opened 2 years ago

Last modified 2 years ago

Element.extend doesn't seem to work in IE sometimes

Reported by: alisey Assigned to: sam
Priority: low Milestone:
Component: Prototype Version:
Severity: trivial Keywords: IE DOM extend
Cc:

Description

Sorry for a large snippet, I could not localize the problem well. In the snippet below IE doesn't extend HTML elements right. Try to load the sample and click an image. The group should be removed. For empty group it works well, but for non-empty group I get an error. Elements have property _extended==true, but all the additional methods are missing. I use prototype 1.5.0 and IE6, IE7 for Windows. The snippet works as intended in FF2 and Opera 9.

For now I've modified Element.extend in my copy of prototype, so that it checks the presence of element.toggle instead of element._extended.

Btw the site is very very slow. And I can't find how do I attach files. So here is it:


<script src="prototype.js">// version. 1.5.0</script>
<script>
function initCl() {
    contacts.each(function(contact){
       moveContact(contact.value.id, contact.value.group);
    });
    addGroup('comeon');
}

function moveContact(contactId, groupName) {
    $('cl_group_'+groupName) || addGroup(groupName);
    $('cl_contacts_'+groupName).innerHTML+= (
        '<div class="contact" id="cl_contact_'+contactId+'">' +
            contacts[contactId].nick +
        '</div>'
    );
}

function addGroup(groupName) {
    $('cl').innerHTML+= (
        '<div class="cl_group" id="cl_group_'+groupName+'">' +
            '<div class="cl_fold">' +
                '<span>'+groupName+'</span>' +
                '<img class="removeGroup" onclick="removeGroup(\''+groupName+'\');">' +
            '</div>' +
            '<div class="cl_contacts" id="cl_contacts_'+groupName+'"></div>' +
        '</div>'
    );
}

function removeGroup(groupName) {
    $('cl_group_' + groupName).remove();
}

contacts = $H({
    1341:{id: 1341, nick: 'alisey', group: 'parasya'},
    1342:{id: 1342, nick: 'sabak', group: 'parasya'},
    1343:{id: 1343, nick: 'kisya', group: 'parasya'}
});
Event.observe(window, 'load', initCl);
</script>

<div id="cl"></div>

Change History

02/06/07 16:52:58 changed by mislav

  • keywords set to IE DOM extend.

Yeah, Trac has issues these days. Thanks for the report, we will investigate. If you find out something in the meantime or even write failing unit tests, be sure to comment here!

02/07/07 00:10:29 changed by mislav

  • status changed from new to closed.
  • resolution set to invalid.

It took me nearly half an hour to figure this one out. This isn't a bug in Prototype, it's a bug in IE which you can easily avoid by fixing your code.

See what are you doing in addGroup():

function addGroup(groupName) {
    $('cl').innerHTML += "... some big HTML chunk ..."
}

Notice the operator you're using: +=. You are concatenating strings of HTML; more precisely, you're overwriting innerHTML of 'cl' element every time you call addGroup().

Why is this too liberal use of innerHTML bad? Well, you're overwriting elements you have written before. Firefox doesn't have problems with it, but to IE you're creating new objects. Those don't have methods glued to them that they had after Element.extend, but they have the old properties (like _extended).

Workaround: don't use innnerHTML like this. Use DOM methods (createElement, appendChild) or Insertion.After.

BTW, thanks for discovering another nasty IE bug ;)

02/07/07 19:53:33 changed by alisey

  • priority changed from normal to low.
  • type changed from defect to enhancement.
  • severity changed from normal to trivial.

Thanks a lot for a quick and irrefragable answer. I was about to post the explanation, but you've already done that :)

Insertion.After is a cure, but what can be done to track down such issues? Maybe Prototype sould check for one of extended methods, as you can see _extended is not 100% reliable.

02/07/07 23:14:15 changed by mislav

  • type changed from enhancement to defect.

I have something in mind for better _extended property, but don't count on it yourself. We won't make that change to encourage this bad innerHTML practice, so better fix your code and forget about this.

Also, this was reported as a defect, let it stay a defect. I closed the ticket, you don't have to change it anymore.

03/11/07 22:40:02 changed by madrobby

  • status changed from closed to reopened.
  • resolution deleted.

We're fixing that Element.extend doesn't barf here.

03/11/07 22:41:10 changed by madrobby

  • status changed from reopened to closed.
  • resolution set to fixed.

Fixed in [6385].