Topic: Run a function after page load -- but not from <body> tag

I'm playing around with my own cheap version of an ajax chat system. 

To start getting chat events, I need to call a javascript function just one time.  I'm currently using <body onload = "...">.  Which is not a great place for this, because I won't know which parts of the website really will need to call that function -- only the ones that have a chat box, should make a call to this function.

So, really, I'd like to stick a call to the function into the div that will do the chatter reporting.  How should I do this?

So I want it in here:

<div id="chatbox">...</div>

and not in the <body> tag.

Thoughts?

Re: Run a function after page load -- but not from <body> tag

You might try sticking the JavaScript code you want to run in a Controller variable (like @js_function_call), then expand that variable inside the <div> using a Ruby snippet, like this:

<div id="chatbox" <%= @js_function_call %> ... </div>

Let me know if that works - it might come in handy elsewhere!  Good luck.

Re: Run a function after page load -- but not from <body> tag

Hmm... interesting.

Actually, it seems that prototype can deal with this sort of thing in a nice way.  See the following documentation: http://www.prototypejs.org/api/event/observe

Re: Run a function after page load -- but not from <body> tag

I know this post is old, but this is a great post to bookmark for different ways to execute javascript or jquery on the load of a webpage: http://webdeveloperpost.com/Articles/Wa … loads.aspx

Re: Run a function after page load -- but not from <body> tag

With prototype:

$(document).observe('dom:ready', function(){
  //your function call here
});

With jQuery:

$(document).ready(function(){
  //your function call here
});