06.07.2017 Views

Mastering JavaScript

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 8<br />

DOM<br />

<br />

window.onload= function() {<br />

var greeting = document.getElementById("greeting");<br />

alert(greeting.innerHTML); //shows "Hello World" alert<br />

}<br />

<br />

<br />

<br />

Hello World!<br />

Earthlings<br />

<br />

<br />

What we discussed so far was the basics of DOM traversal in <strong>JavaScript</strong>. When<br />

the DOM gets complex and you want sophisticated operations on the DOM, these<br />

traversal and access functions seem limiting. With this basic knowledge with us, it's<br />

time to get introduced to a fantastic library for DOM traversal (among other things)<br />

called jQuery.<br />

jQuery is a lightweight library designed to make common browser operations easier.<br />

Common operations such as DOM traversal and manipulation, event handling,<br />

animation, and Ajax can be tedious if done using pure <strong>JavaScript</strong>. jQuery provides<br />

you with easy-to-use and shorter helper mechanisms to help you develop these<br />

common operations very easily and quickly. jQuery is a feature-rich library, but as<br />

far as this chapter goes, we will focus primarily on DOM manipulation and events.<br />

You can add jQuery to your HTML by adding the script directly from a content<br />

delivery network (CDN) or manually downloading the file and adding it to the<br />

script tag. The following example shows you how to download jQuery from Google's<br />

CDN:<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

[ 185 ]<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!