官术网_书友最值得收藏!

Time for action—downloading and installing the MooTools Core

The following steps will help you to download and install MooTools Core:

  1. Go to the MooTools Download page located at http://mootools.net/download. You'll see three options, choose the Uncompressed version.
    Time for action—downloading and installing the MooTools Core
  2. Create an HTML document, call it whatever you want, but I'll call mine index.html, as shown:
    Time for action—downloading and installing the MooTools Core
  3. Install MooTools by referencing it in the<head> of your HTML document with the following code:
    <script type="text/javascript" src="mootools-1.2.1-core-nc.js"></script>
    

    Right below the MooTools script reference, place the following block of code:

    <script type="text/javascript"> window.addEvent('domready', function() { alert("Hello World!"); }); </script>
    
  4. Test your code by opening your HTML document in your favorite web browser. If everything went according to plan, you should see something like this:
    Time for action—downloading and installing the MooTools Core

What just happened?

We've just downloaded and installed MooTools by referencing it in an HTML document that we created and then tested to see if we referenced the file correctly by writing a line of JavaScript that displays "Hello World" in an alert box.

For your reference, your HTML markup should look similar to the following:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Installing MooTools</title>
<script type="text/javascript" src="mootools-1.2.1-core-nc.js"> </script>
<script type="text/javascript">
window.addEvent('domready', function() {
alert("Hello World!");
});
</script>
</head>
<body>
</body>
</html>

If all went well, congratulations! You're on your way to becoming a MooTools rockstar!

Different MooTools downloads

When you went to the MooTools download page, you had three options: YUI Compressor, JSMin, and Uncompressed. These options have the same source code and differ only in the source code format. The YUI Compressor and JSMin versions are compressed. They are smaller in file size because unnecessary characters (such as those involved in code formatting, like tabs, extra spaces, and inline comments) have been removed.

The process of removing extraneous characters in the source code is called Minification. The trade-off in minifying your source code is that it's virtually unreadable, so we downloaded the Uncompressed version, in case you wanted to see how MooTools works by studying the source code.

The domready event listener

When writing MooTools code, you will often want to execute the code as soon as possible, otherwise it'll wait until other webpage components are loaded before it runs.

Let's take a closer look at the code we wrote earlier:

window.addEvent('domready', function() {
alert('Hello World');
});

In this block of code, we used the addEvent method and we tell it to watch out for the event that the window's DOM is ready ('domready'). Once the DOM is ready, we ask the method to run the following function:

function() {
alert("Hello World");
}

We will discuss the MooTools syntax and concepts in detail in Chapter 2, but for now, let's just say that all code we write throughout this book will be wrapped inside the domready event listener so they are executed as soon as possible.

主站蜘蛛池模板: 高密市| 肥西县| 大埔县| 洞口县| 新密市| 茶陵县| 彰武县| 普宁市| 吉隆县| 万州区| 百色市| 桂阳县| 桐庐县| 门头沟区| 甘孜县| 沛县| 大石桥市| 金昌市| 旬邑县| 陈巴尔虎旗| 开阳县| 双流县| 海原县| 航空| 乐亭县| 江源县| 乌什县| 孟津县| 乌海市| 仲巴县| 太仆寺旗| 宁城县| 三穗县| 宁国市| 长兴县| 焦作市| 苗栗县| 开远市| 广丰县| 静乐县| 平利县|