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:

謝詩涵 said...

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

林淑凡 said...

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

賢諭 said...

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

銘雅婷木 said...

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

Glennie9654 said...

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

家祥 said...

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

雅婷 said...

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

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

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

Dorath政儒yN_ said...

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

張孟勳 said...

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

文行文行 said...

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

萱祥 said...

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

蔣筱芸 said...

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

亦妮亦妮 said...

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

靜怡 said...

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

夏康如 said...

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

彥佳dk茹胡陳lil雨 said...

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

陳韋夏陳韋夏益東富益東富 said...

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

茂慧茂慧 said...

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

JasonBirk佳琪 said...

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

柯凡豐 said...

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

婷珊 said...

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

淑慧淑慧 said...

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

佳皓 said...

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

智宜宜 said...

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

李吳文云威昌 said...

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

陳柏毅 said...

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

劉黃志宇建娥 said...

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

吳承侯政霖虹 said...

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

凱v胡倫 said...

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

偉曹琬 said...

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

秀李李迪秀李李迪 said...

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

文汝劉文汝劉文汝劉 said...

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

翊翊翊翊張瑜翊翊翊 said...

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

柔于真于真于真林姍 said...

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