开发者

Prototype Javascript, working in Mozilla but not in IE

I am using JavaScript Library from prototype v.1.6.0.3 I found out that the basic notation $ is working fine in Mozilla, but not in IE6.

Here is simple example

  • Element.hide('div123'); --> working at IE & Mozilla
  • $('div123').hide(); --> 开发者_JAVA技巧working only at Mozilla

I am wondering what i am missing here, as I also have some other cases that not working in IE like drag & drop (it never get dropped in IE, but fine in Mozilla). Though from my understanding Prototype is "support" IE ?


I don't think the error is in the posted code; both versions work for me and neither of those calls do anything much different on the different browsers.

However Prototype does work very differently in IE and Mozilla, and this may be causing differences elsewhere in your code.

In Mozilla, Prototypre adds its methods to the prototypes of HTMLElement et al, so that all DOM objects can have these methods called on them directly. However this is not possible in IE(*), so to cover all browsers you have to ‘augment’ each node you want to call methods on, either explicitly by calling Element.extend on it, or implicitly by using one of Prototype's own methods, such as the $ function, to get a handle on the object:

document.getElementByID('foo').hide(); // ok on Mozilla, fail on IE

$('foo').hide(); // ok everywhere

Element.hide('foo'); // ok everywhere

Element.extend(document.getElementByID('foo'));
document.getElementByID('foo').hide(); // ok everywhere

This is actually one of Prototype's worst features, because you can write a load of code in Mozilla that will fail in IE and not notice: it's not so much hiding the browser differences as amplifying them.

What's worse, since many of Prototype's own methods extend objects implicitly, and because once extended those Nodes retain their extensions, it is very easy to get situations where in IE your code that forgets to extend an element will usually work because something else extended it already, but in some rarer circumstance will blow up. This ain't good for debugging.

(*: not IE's fault. In the ECMAScript standard there is no expectation that you'll be able to alter the prototypes of ‘host objects’ like the DOM, even if you can get a handle on their constructor function, which is also not specified to exist in any particular place. Prototype is taking advantage of a non-standard feature to make code potentially look nicer, but you can't take advantage of that in practice, sadly.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜