Watching:

Writing a Prototype-enabled page


show more Writing a Prototype-enabled page provides you with in-depth training on Developer. Taught by Joe Marini as part of the Prototype and script.aculo.us Essential Training show less
please wait ...

Writing a Prototype-enabled page

Let's just jump right in and build our first Prototype enabled web page. So before we do that, a quick word about tools. For the remainder of this course, I'm going to be using a tool named Aptana Studio. And Aptana Studio is a really good cross-platform IDE for working with web content like HTML and JavaScript files. And it's freely available from aptana.org. The reason I chose to use it is because it's cross-platform, so if you're following me along on a Mac or Windows, it doesn't really matter.

You can just download it to your computer free of charge and follow along with me. Now, since Prototype and script.aculo.us are just JavaScript files and we'll be working with HTML, you can use whatever IDE you are happy and familiar with. I just want to let you know that I'm going to be using Aptana. So you can download it here from the aptana.org website. You just click Download Now. You can see that it's available for various different platforms, whether standalone, or if you already have Eclipse installed, and you can get it for Windows, Mac, or Linux.

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.

Now, the Finished file is the completed example. And you can go ahead and jump forward to that if you just want to see how everything works, or you can follow along with me and we can work together on the Start version. So I'm going to open up the Start. And you can see that this is a pretty plain HTML page. So to use Prototype in our web page, I just have to include it using a script tag. So I'm going to insert a script and let's say type equals text/javascript.

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.

All right. So now let's just do a quick simple example to show how Prototype works. So I'm going to enter another script tag, type equals, once again, text/javascript. So I'll put our example code in here. What I'm going to do is show an example of how to fire off a script when the page loads. And ordinarily, you would do this by doing something like body and then you'd probably do something like onload equals function.

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.

Writing a Prototype-enabled page
Video duration: 5m 44s 6h 22m Beginner

Viewers:

Writing a Prototype-enabled page provides you with in-depth training on Developer. Taught by Joe Marini as part of the Prototype and script.aculo.us Essential Training

Subjects:
Developer Web
Software:
Ajax Prototype
Author:
please wait ...