A few lessons learned on prototype.js and RoR

A few lessons learned from my recent work on prototype.js and Ruby on Rails

1) "class" is a reserved word for IE
"class" is a reserved word for IE. If you use "class" as variable name in your JavaScript such as this (example using prototype.js):
var highlight_span= new Element('span', { margin: '0', class: 'highlight' }); 

It works in Firefox but gives you an error in IE. The work around is:
var highlight_span= new Element('span', { margin: '0'}); highlight_span.addClassName("highlight"); 

2) IE requires 'tbody' in dynamically generated DOM
If you use dynamic JavaScript, such as "new Element(..)", to generate a table in layout, make sure the 'tbody' element is generated explicitly. Otherwise, the table won't show up in IE. Firefox does not seem to care about the missing of 'tbody'. Static coded table without 'tbody' works fine in IE.

3) IE requires you to explicitly register event handler
Following code works in Firefox, but not IE:
var a = new Element('a', {onclick: handler}); 

You have to use "observe" to explicitly register the handler function:
var a = new Element('a').observe('click', handler); 

4) You cannot set cookie value to int in RoR
This code won't work:
cookies[:worker] = @worker.id 

Instead, you need to say:
cookies[:worker] = @worker.id.to_s 

No comments: