Join Joe Marini for an in-depth discussion in this video Writing a Prototype-enabled page, part of Prototype and script.aculo.us Essential Training.
So that's the tool that I'm going to be using. Now, I've already installed it, so I'm going to go ahead and switch over to it. So this is Aptana Studio. And this is the tool that I'm going to be using for the examples. So I'm going to go ahead and open up our example file. So I'm going to open up the file. Now, if you have the exercise files, you'll see that in the exercise_files folder, I have arranged all of the examples according to chapters. And here in the first chapter for the overview, I've got two examples, the FirstPrototype Start and Finished.
And now I need to point the source attribute to the location where the prototype.js file is kept. Now, I've put it at the root level of the exercise_files folder. That way it can be accessed by all of the examples using a relative URL format. So I'm just going to type "../". And you can see that there it is right there, the prototype.js file. So I'll double click that and close the tag. And that's pretty much all there's to it. Now our prototype is embedded in the page. And we can Save.
The problem with this approach is that the onload event on the body tag only fires once all of the body's content has finished downloading, and that includes images. So if you have a bunch of images in your page or a lot of content, then this event isn't going to fire until-- It could be sometime before all the content downloads, and in the meantime, your user is trying to interact with the page and nothing is happening. Well, prototype fixes that problem by making available a cross-browser event called dom:loaded, which fires much, much sooner than the body's onload event.
The way that works is you observe the document event for the dom:loaded. Now, I'm not going to expect that you understand everything that I'm about to type right away, but we will cover all of this in subsequent lessons. Just for the moment, follow along with me as I explain what's going on here. So what I need to do is write document. observe, and this is how in Prototype you register for event handlers. You type the name of the element, in this case document, and then the observe function is what observes events.
So the event I'm interested in observing is the dom:loaded event. And then I need to provide a function that's going to run when the dom gets loaded. So I'm just going to put in an anonymous function right here. And I'm going to write just a simple alert that says "The page has loaded! You're using Prototype!" So that's all there is to it.
That's our first Prototype-enabled page. So let's jump over to the browsers and see if it works. I'm going to go to the exercise_ files folder. Here's the folder. So I'm just going to double-click on this guy and that should bring up Firefox. And you can see that when Firefox loads, we get our alert. So let's make sure that this is running properly cross-browser. I'll bring up IE next. Okay. So here I am in the examples. I'm going to right-click. I'm going to choose Open with Internet Explorer.
And you can see I get the same result. So we have a cross-browser way now of detecting when the page has loaded. And that's our first example using Prototype.
- Retrieving and manipulating web page elements
- Handling web page events and creating custom events
- Traversing the DOM
- Creating and handling AJAX requests
- Using script.aculo.us controls such as Autocompleters, Sliders, and Visual Effects
- Building complex page features like data filtering and image rotators