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.

<strong>JavaScript</strong> Theming<br />

As we have already seen, when the page is delivered <strong>and</strong> loaded, <strong>JavaScript</strong> can start<br />

doing its thing. It can rearrange the page, add new data, hide old data, <strong>and</strong> create an<br />

interactive environment in which the user's input can change what is displayed.<br />

The server's theme engine is responsible for taking <strong>Drupal</strong> data <strong>and</strong> laying it out<br />

for display. Once loaded on the client side, <strong>JavaScript</strong> can change that display. This<br />

means even JavaScrip can be<strong>com</strong>e responsible for generating the look <strong>and</strong> feel of a<br />

page. Therefore, <strong>JavaScript</strong> must be able to generate <strong>and</strong> manipulate HTML <strong>and</strong> CSS.<br />

How does <strong>JavaScript</strong> h<strong>and</strong>le this task? In the past, it h<strong>and</strong>led it in one of the two<br />

not-very-graceful ways.<br />

The first way was to embed strings inside of application logic. This led to code that<br />

looked something like this:<br />

function calculateSum() {<br />

var out = '';<br />

var total = 0;<br />

if (arguments.length == 0) {<br />

out += 'Error: No data given.';<br />

}<br />

else if (arguments.length == 1) {<br />

out += 'Error: Cannot sum only one number.';<br />

}<br />

else {<br />

out += 'The sum is: ';<br />

for(var i = 0; i < arguments.length; ++i) {<br />

total += arguments[i];<br />

}<br />

out += new String(total) + '';<br />

}<br />

out += '';<br />

document.getElementById('sum').innerHTML = out;<br />

return total;<br />

}<br />

Simply speaking, the purpose of this demonstration function is to sum all of the<br />

arguments passed to it, <strong>and</strong> then place the result inside of the element with the id sum.<br />

Passing an arbitrary number of arguments in <strong>JavaScript</strong><br />

<strong>JavaScript</strong> supports passing a function, a variable number of arguments<br />

(sometimes referred to as varargs). Every function has access to a variable<br />

called arguments, which is an array-like object containing all of the<br />

parameters (in order) that were passed into the function.<br />

[ 156 ]<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!