# Ajax call once on page load

**URL:** <https://www.ruby-forum.com/t/ajax-call-once-on-page-load/150034>\
**Category:** Rails\
**Created:** [October 2, 2008, 5:39pm UTC](https://www.ruby-forum.com/t/ajax-call-once-on-page-load/150034 "2008-10-02T17:39:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [October 2, 2008, 5:39pm UTC](https://www.ruby-forum.com/t/ajax-call-once-on-page-load/150034/1 "2008-10-02T17:39:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [October 2, 2008, 6:04pm UTC](https://www.ruby-forum.com/t/ajax-call-once-on-page-load/150034/2 "2008-10-02T18:04:07Z")

</div>

On Oct 2, 4:39 pm, Tim C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 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

---

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [October 2, 2008, 11:57pm UTC](https://www.ruby-forum.com/t/ajax-call-once-on-page-load/150034/3 "2008-10-02T23:57:28Z")

</div>

> 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](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 C. wrote:

> On Oct 2, 4:39ï¿½pm, Tim C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > 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
