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() {
    alert("Hello");
}

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() {
    alert("Hello");
}

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:

alert("Hello");

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?

36 comments:

Anonymous said...

may the blessing be with you.........................................

Anonymous said...

人生是故事的創造與遺忘。......................................................

Anonymous said...

先告訴自己希望成為什麼樣的人,然後一步一步實踐必要的步驟。........................................

Anonymous said...

April showers bring May flowers. ........................................

Anonymous said...

I love readding, and thanks for your artical. ........................................

家祥 said...

Thank you for Posts~............................................................

雅婷 said...

喜歡你的部落格,留言請您繼續加油.............................................

Anonymous said...
This comment has been removed by a blog administrator.
于呈均名 said...

your english is incredible............................................................

Dorath政儒yN_ said...

The shortest answer is doing. ............................................................

Anonymous said...

找一個懂妳的人也期許自己做一個人懂別人的人 ..................................................

Anonymous said...

你不能改變容貌~~但你可以展現笑容..................................................................

萱祥 said...

好文章給我好心情,謝謝分享~~ .................................................................

Anonymous said...

在莫非定律中有項笨蛋定律:「一個組織中的笨蛋,恆大於等於三分之二。」......................................................................

Anonymous said...

死亡是悲哀的,但活得不快樂更悲哀。......................................................................

Anonymous said...

生存乃是不斷地在內心與靈魂交戰;寫作是坐著審判自己。......................................................................

Anonymous said...

成熟,就是有能力適應生活中的模糊。.................................................................

Anonymous said...

It takes all kinds to make a world.............................................................

Anonymous said...

安安!剛開始玩這個,來這裡逛一下^^............................................................

Anonymous said...

要拼才會贏,生活就像戰爭…什麼時候才有和平啊…>"<..................................................

Anonymous said...

一個人的際遇在第一次總是最深刻的,有時候甚至會讓人的心變成永遠的絕緣。............................................................

Anonymous said...

人生之中,比冒險更危險的一件事:不去冒險。..................................................

Anonymous said...

良好的開端,已是成功的一半。.......................................................

Anonymous said...

Offence is the best defence...................................................

Anonymous said...

.有價值的東西,都要付出代價。-................................................

Anonymous said...

愛情是一位偉大的導師,教我們重新作人..................................................................

Anonymous said...

老天爺賦予了強者的能力,就是要他比弱者多擔待.......................................................

Anonymous said...

你的部落格不錯哦,支持!!!!@@ ..................................................................

Anonymous said...

人生是一連串的課程,必須活過才能明白。.......................................................

Anonymous said...

新手上路哦~請大家多支持(・ˍ・*) ............................................................

Anonymous said...

真正仁慈的人,會忘記他們做過的善行,他們全心投入現在的工作,過去的事已被遺忘。.................................................

Anonymous said...

與朋友在一起,分擔的痛苦是減半的痛苦,分享的快樂是加倍的快樂。......................................................................

Anonymous said...

看著你的BLOG 好多朋友都回應 真厲害..................................................

Anonymous said...

這麼用心的經營你的文章, 當然值得我們留連拜訪的!...............................................................

Anonymous said...

唯有用熱情、用智慧去觀察事物,這事物才會把他的秘密,洩漏給我們......................................................................

Anonymous said...

感動 + 感恩 + 感謝(。-ˍ-。 )............................................................