JavaScript Libraries

« Previous

Next Chapter »

 

JavaScript libraries - jQuery, Prototype, MooTools.

 

JavaScript Frameworks (Libraries)

Advanced JavaScript programming (especially the complex handling of browser differences), can often be very difficult and time-consuming to work with.

To deal with these difficulties, a lot of JavaScript (helper) libraries have been developed.

These JavaScript libraries are often called JavaScript frameworks.

In this tutorial, we will take a look at some of the most popular JavaScript frameworks:

  • jQuery
  • Prototype
  • MooTools

All of these frameworks have functions for common JavaScript tasks like animations, DOM manipulation, and Ajax handling.

In this tutorial we will teach you how start using them, to make JavaScript programming easier, safer, and much more exciting.

 

jQuery

jQuery is the most popular JavaScript framework on the Internet today.

It uses CSS selectors to access and manipulate HTML elements (DOM Objects) on a web page. 

jQuery also provides a companion UI (user interface) framework and numerous other plug-ins.

Many of the largest companies on the Web use jQuery:

  • Google
  • Microsoft
  • IBM
  • Netflix

You will find an excellent jQuery Tutorial here at W3Schools.

 

Prototype

Prototype is a JavaScript library that provides a simple API to perform common web tasks.

API is short for Application Programming Interface. It is a library of properties and methods for manipulating the HTML DOM.

Prototype enhances JavaScript by providing classes and inheritance.

 

MooTools

MooTools is also a framework that offers an API to make common JavaScript programming easier.

MooTools also includes some lightweight effects and animation functions.

 

Other Frameworks

Here are some other frameworks not covered in this short overview:

YUI - The Yahoo! User Interface Framework is a large library that covers a lot of functions, from simple JavaScript utilities to complete internet widgets.

Ext JS - Customizable widgets for building rich Internet applications.

Dojo - A toolkit designed around packages for DOM manipulation, events, widgets, and more.

script.aculo.us - Open-source JavaScript framework for visual effects and interface behaviors.

UIZE - Widgets, AJAX, DOM, templates, and more.

 

CDN - Content Delivery Networks

You always want your web pages to be as fast as possible. You want to keep the size of your pages as small as possible, and you want the browser to cache as much as possible.

If many different web sites use the same JavaScript framework, it makes sense to host the framework library in a common location for every web page to share.

A CDN (Content Delivery Network) solves this. A CDN is a network of servers containing shared code libraries.

Google provides a free CDN for a number of JavaScript libraries, including:

  • jQuery
  • Prototype
  • MooTools
  • Dojo
  • Yahoo! YUI

To use a JavaScript framework library in your web pages, just include the library in a <script> tag:

Including jQuery

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 

 

 

 

 

JavaScript - Testing jQuery

« Previous

Next Chapter »

 

Testing JavaScript Framework Libraries - jQuery

 

Including jQuery

To test a JavaScript library, you need to include it in your web page.

To include a library, use the <script> tag with the src attribute set to the URL of the library:

Including jQuery

<!DOCTYPE html>
<html>

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>

<body>
.
.
.

 

 

jQuery Described

The main jQuery function is the $() function (the jQuery function). If you pass DOM objects to this function, it returns jQuery objects, with jQuery functionality added to them.

jQuery allows you to select elements by CSS selectors.

In JavaScript, you can assign a function to handle the window's load event:

The JavaScript Way:

function myFunction() {
    var obj = document.getElementById("h01");
    obj.innerHTML = "Hello jQuery";
}
onload = myFunction;

The jQuery equivalent is different:

The jQuery Way:

function myFunction() {
    $("#h01").html("Hello jQuery");
}
$(document).ready(myFunction);

The last line of the code above, passes the HTML DOM document object to jQuery: $(document).

When you pass DOM objects to jQuery, jQuery returns new jQuery objects wrapped around the HTML DOM objects.

The jQuery function returns a new jQuery object, where ready() is a method.

Since functions are variables in jQuery, myFunction can be passed as a variable to the jQuery ready() method.  

 

jQuery returns a jQuery object, different from the DOM object that was passed.
The jQuery object has properties and methods, different from the DOM object.
You cannot use HTML DOM properties and methods on jQuery objects.

 

 

Testing jQuery

Try the following example:

Example

<!DOCTYPE html>
<html>

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<script>
function myFunction() {
    $("#h01").html("Hello jQuery")
}
$(document).ready(myFunction);
</script>
</head>

<body>
<h1 id="h01"></h1>
</body>
</html> 


Try it Yourself » 

Also try this one:

Example

<!DOCTYPE html>
<html>

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<script>
function myFunction() {
    $("#h01").attr("style", "color:red").html("Hello jQuery")
}
$(document).ready(myFunction);
</script>
</head>

<body>
<h1 id="h01"></h1>
</body>
</html> 

 

 

 

JavaScript - Testing Prototype

« Previous

Next Chapter »

 

Testing JavaScript Framework Libraries - Prototype

 

Including Prototype

To test a JavaScript library, you need to include it in your web page.

To include a library, use the <script> tag with the src attribute set to the URL of the library:

Including Prototype

<!DOCTYPE html>
<html>

<head>
<script src="http://ajax.googleapis.com/ajax/libs/prototype/1.7.2.0/prototype.js"></script>
</head>

<body>
.
.
.

 

 

Prototype Described

Prototype provides functions to make HTML DOM programming easier.

Like jQuery, Prototype has its $() function.

The $() function accepts HTML DOM element id values (or DOM elements), and adds new functionality to DOM objects.

Unlike jQuery, Prototype has no ready() method to take the place of window.onload(). Instead, Prototype adds extensions to the browser and the HTML DOM.

In JavaScript, you can assign a function to handle the window's load event:

The JavaScript Way:

function myFunction() {
    var obj = document.getElementById("h01");
    obj.innerHTML = "Hello Prototype";
}
onload = myFunction;

The Prototype equivalent is different:

The Prototype Way:

function myFunction() {
    $("h01").insert("Hello Prototype!");
}
Event.observe(window, "load", myFunction);

Event.observe() accepts three arguments: 

  • The HTML DOM or BOM (Browser Object Model) object you want to handle
  • The event you want to handle
  • The function you want to call

 

Testing Prototype

Try the following example:

Example

<!DOCTYPE html>
<html>

<head>
<script src="http://ajax.googleapis.com/ajax/libs/prototype/1.7.2.0/prototype.js"></script>
<script>
function myFunction() {
    $("h01").insert("Hello Prototype!");
}
Event.observe(window, "load", myFunction);
</script>
</head>

<body>
<h1 id="h01"></h1>
</body>
</html> 


Try it Yourself » 

Also try this one:

Example

<!DOCTYPE html>
<html>

<head>
<script src="http://ajax.googleapis.com/ajax/libs/prototype/1.7.2.0/prototype.js"></script>
<script>
function myFunction() {
    $("h01").writeAttribute("style", "color:red").insert("Hello Prototype!");
}
Event.observe(window, "load", myFunction);
</script>
</head>

<body>
<h1 id="h01"></h1>
</body>
</html> 

 

 

JavaScript Summary

This tutorial has taught you how to add JavaScript to your HTML pages, to make your web site more dynamic and interactive.

You have learned how to create responses to events, validate forms and how to make different scripts run in response to different scenarios.

You have also learned how to create and use objects, and how to use JavaScript's built-in objects.

For more information on JavaScript, please look at our JavaScript examples and our JavaScript reference.

 

Now You Know JavaScript, What's Next?

The next step is to learn about the HTML DOM, jQuery, and AJAX.

If you want to learn about server-side scripting, the next step is to learn ASP

jQuery

jQuery is a JavaScript Library.

jQuery greatly simplifies JavaScript programming.

If you want to learn more about jQuery, please visit our jQuery tutorial.

AJAX

AJAX = Asynchronous JavaScript and XML.

AJAX is not a new programming language, but a new way to use existing standards.

AJAX is about exchanging data with a server, and update parts of a web page - without reloading the whole page.

Examples of applications using AJAX: Google Maps, Gmail, Youtube, and Facebook tabs.

If you want to learn more about AJAX, please visit our AJAX tutorial.

Web Building

In this tutorial we have created dynamic web pages by using scripts on the client (in the browser).

Web pages can also be made more dynamic by using scripts on the server. 

With server-side scripting you can edit, add, or change any web page content. You can respond to data submitted from HTML forms, access data or databases and return the results to a browser, and customize pages for individual users.