Ajax call once on page load

I want to run an Ajax call but I don't want to do it through a link (link_to_remote), through a form (form_remote_tag) or periodically (periodically_call_remote). I just want to make the call once and I want to happen automatically after the page has finished loading. I'd like to write this using the prototype helpers, what helper do I need to use? Thanks

I want to run an Ajax call but I don't want to do it through a link (link_to_remote), through a form (form_remote_tag) or periodically (periodically_call_remote). I just want to make the call once and I want to happen automatically after the page has finished loading. I'd like to write this using the prototype helpers, what helper do I need to use?

remote_function generates the javascript for making an ajax request.

Fred

remote_function generates the javascript for making an ajax request.

Although you could certainly use remote_function helper to achieve the effect you want, if you are manipulating the DOM with your AJAX call you must ensure that the DOM has been loaded before you AJAX call occurs.

I prefer to do this using an unobtrusive JavaScript technique as explained in the Prototype documentation: http://www.prototypejs.org/api/event/observe

Event.observe(window, 'load', function() {   Event.observe('signinForm', 'submit', checkForm); });

The important line from the example above is the first line. It creates a function that is executed after the page is fully loaded. The documentation goes on to explain that observing the window's load event waits for the entire page to load, rather than executing just after the DOM has been fully loaded. This could be an issue if your page is complex and has a lot of images and other resources to load. There is a link to a blog that explains how to solve that if necessary.

Apparently JQuery has these workarounds built in already, but I'm only scratching the surface of JQuery, so I'm not sure how well that works in all the various browsers. But, I'm guessing they have figured it out.

Frederick Cheung wrote: