jBoxer

I change the directions of small pieces of metal for a living.

Prototype analog to jQuery's $(document).ready

| Comments

I have a lot of experience with jQuery, but less with Prototype. Recently, I needed to add some event handlers to some elements in a Ruby on Rails app I’m building. I searched for how to do the equivalent of jQuery’s $(document).ready() function in Prototype so that I could add the handlers after the document loaded, but most of the guides I found were out of date (I’m running Prototype 1.6.0.3, and I don’t know which version these guides were for, but they all made my Javascript console angry).

Eventually, I was able to piece it together after digging through the depths of the Prototype API documentation. It’s actually very simple:

1
2
3
document.observe('dom:loaded', function(){
  // do yo thang...
});

Wrap whatever you’re doing with that, and it won’t be run until the document is loaded.

Comments