06.05.2013 Views

Drupal 6 JavaScript and jQuery - Ebook-Cours.com

Drupal 6 JavaScript and jQuery - Ebook-Cours.com

Drupal 6 JavaScript and jQuery - Ebook-Cours.com

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Working with <strong>JavaScript</strong> in <strong>Drupal</strong><br />

How <strong>Drupal</strong> h<strong>and</strong>les <strong>JavaScript</strong><br />

How is <strong>JavaScript</strong> typically used? Mostly, it is used to provide additional functionality<br />

to a web page, which is usually delivered to a web browser as an HTML document.<br />

The browser receives the HTML from the server <strong>and</strong> then begins the process of<br />

displaying the page. During this parsing <strong>and</strong> rendering process, the browser may<br />

request additional resources from the server such as images, CSS, or Flash. It then<br />

incorporates these elements into the document displayed to the user.<br />

In this process, there are two ways that <strong>JavaScript</strong> code can be sent from the server to<br />

the browser. First, the code can be placed directly inside the HTML. This is done by<br />

inserting code inside the <strong>and</strong> tags:<br />

<br />

alert('hello world');<br />

<br />

This is called including the script inline.<br />

Second, the code can be loaded separately from the rest of the HTML. Again, this is<br />

usually done using the <strong>and</strong> tags. However, instead of putting<br />

the code between the tags, we use the src attribute to instruct the browser to retrieve<br />

an additional document from the server.<br />

<br />

In this example, src="some/script.js" points the browser to an additional script<br />

file stored on the same server as the HTML document in which this script tag is<br />

embedded. So, if the HTML is located at http://example.<strong>com</strong>/index.html, the<br />

browser will request the script file using the URL http://example.<strong>com</strong>/some/<br />

script.js.<br />

The tag is required<br />

When XML was first st<strong>and</strong>ardized, it introduced a shorth<strong>and</strong> notation<br />

for writing tags that have no content. Instead of writing , one<br />

could simply write . While this notation is supported by all modern<br />

mainstream browsers, it cannot be used for <br />

tags. Some browsers do not recognize <strong>and</strong> expect that any<br />

tag will be ac<strong>com</strong>panied by a closing tag even if<br />

there is no content between the tags.<br />

If we were developing static HTML files, we would simply write HTML pages<br />

that include tags anytime we needed to add some <strong>JavaScript</strong><br />

to the page. But we're using <strong>Drupal</strong>, not static HTML, <strong>and</strong> the process for adding<br />

<strong>JavaScript</strong> in this environment is done differently.<br />

[ 30 ]<br />

This material is copyright <strong>and</strong> is licensed for the sole use by Richard Ostheimer on 18th June 2009<br />

2205 hilda ave., , missoula, , 59801

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

Saved successfully!

Ooh no, something went wrong!