Wednesday, March 31, 2010

Javascript in Rails Partial

Javascript function defined in a partial sometimes doesn't work (I've seen both worked case and non-worked case). I have a partial named _v_1_0_edit.html.erb; its parent page is index.html.erb. The javascript portion is below:

function my_alert() {

If the above is inside a partial, it doesn't work when I call my_alert() from a onclick event of a button on the partial; it is as if the function is not defined. But if the above is inside the parent, it works.

However, if I change the function definition to below:

my_alert = function my_alert() {

It works if it's defined in a partial (But it doesn't work in IE).

If I just throw the below on a view:


It will show the alert message no matter it's on the partial or the parent.

I need to do more research on this. I think as long as the defined function is shown on page source, it should work. Can I have a script
section inside a div?