Forum: Ruby on Rails Is there a rails equivalent of <body onload="do_stuff()">

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cb880dd8c8c209924ebb57edbb4fcd06?d=identicon&s=25 Jim Burgess (jim_in_germany)
on 2009-05-30 11:10
Hi,

I want to call a javascript function after my page has loaded.
Currently I am using:
<body onload="do_stuff()">
Is there a neater / tidier way to do this using any rails specific
commands?

I found this:

document.observe('dom:loaded', function() {
$('state_field').hide();
$('person_country_id').observe('change', countrySelected);
});

which is part of a tutorial for creating dynamic drop down menus, but
when I, for example, try:

document.observe('dom:loaded', function() {
alert('hello');
});

this has no effect, rather it is output as html, as though I had witten
<p>document.observe('dom:loaded', function() {alert('hello');});</p>

Grateful for any help.
Cb880dd8c8c209924ebb57edbb4fcd06?d=identicon&s=25 Jim Burgess (jim_in_germany)
on 2009-05-30 11:12
Sorry, I forgot the <script> tags.
Now eveything works.
Forget I spoke :-)
Dd2d775dea75b381edb1bbf0600a0907?d=identicon&s=25 Marnen Laibow-Koser (marnen)
on 2009-05-30 15:12
Jim Burgess wrote:
> Sorry, I forgot the <script> tags.
> Now eveything works.

Excellent!  Now take the JS out of the HTML and put it in an external
file.

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
Cb880dd8c8c209924ebb57edbb4fcd06?d=identicon&s=25 Jim Burgess (jim_in_germany)
on 2009-05-30 15:23
> Excellent!  Now take the JS out of the HTML and put it in an external
> file.

Cool, that works.
Thanks a lot.
8f6f95c4bd64d5f10dfddfdcd03c19d6?d=identicon&s=25 Rick Denatale (rdenatale)
on 2009-05-30 18:29
(Received via mailing list)
On Sat, May 30, 2009 at 9:23 AM, Jim Burgess
<rails-mailing-list@andreas-s.net> wrote:
>
>> Excellent!  Now take the JS out of the HTML and put it in an external
>> file.
>
> Cool, that works.
> Thanks a lot.
> --

You might want to have a look at Low Pro

http://www.danwebb.net/2006/9/3/low-pro-unobtrusiv...

It was built for prototype, but there's a version for use with jquery as
well
http://www.danwebb.net/2008/2/3/how-to-use-low-pro...

Although I haven't used that.
--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
This topic is locked and can not be replied to.