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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Drupal</strong> Behaviors<br />

When drupal.js loads, it will evaluate whether the <strong>JavaScript</strong> implementation<br />

supports DOM manipulation of the sort that <strong>jQuery</strong> <strong>and</strong> drupal.js rely upon. If the<br />

correct functions exist then this flag will be set to true. Otherwise, the flag will be set<br />

to false.<br />

Internally, <strong>Drupal</strong> uses this flag to determine if behaviors are supported. If <strong>Drupal</strong>.<br />

jsEnabled is false, then <strong>Drupal</strong>.attachBehaviors() doesn't attempt to attach any<br />

behaviors. It just silently returns.<br />

Also, <strong>Drupal</strong> uses this flag to set a cookie. If <strong>Drupal</strong>.jsEnabled is true, a cookie is<br />

set which indicates that <strong>JavaScript</strong> support is sufficient. This way, server-side code<br />

can send back appropriate responses based on a browser's <strong>JavaScript</strong> capabilities.<br />

(This cookie is named has_js, <strong>and</strong> is available in PHP using $_COOKIE['has_js'].)<br />

Feel free to use <strong>Drupal</strong>.jsEnabled whenever you are concerned that a browser<br />

might not support the necessary <strong>JavaScript</strong> for DOM manipulations. But don't get<br />

overly concerned about checking with the use of this flag. Most <strong>jQuery</strong> functions will<br />

silently fail if <strong>JavaScript</strong> support isn't good enough, <strong>and</strong> the main <strong>Drupal</strong> features<br />

(like behaviors) will be skipped as well.<br />

The <strong>Drupal</strong>.checkPlain() function<br />

(<strong>and</strong> the <strong>jQuery</strong> alternative)<br />

The first function we will look at is the <strong>Drupal</strong>.checkPlain() function. If you have<br />

already written some <strong>Drupal</strong> PHP code, you will probably recognize the name.<br />

The <strong>Drupal</strong>.checkPlain() function takes a string <strong>and</strong> prepares it for display,<br />

escaping symbols that have a special meaning in HTML. While the use of the term<br />

check implies that this will return a Boolean value (true if the text is plain, false if<br />

otherwise), it actually performs the escaping, <strong>and</strong> returns the escaped string.<br />

So what does it escape? Let's look at an example. While viewing a node on our<br />

<strong>Drupal</strong> system, we can use the Firebug console to manipulate the document:<br />

>>> myString = "A string with HTML embedded.";<br />

"A string with HTML embedded."<br />

>>> $('.node .content').html(myString);<br />

Object length=1<br />

The first line creates a new string named myString. Note that the contents of this<br />

string contain embedded HTML: A string with HTML embedded<br />

In the second line we use <strong>jQuery</strong> to find the main content section for the node<br />

displayed on the current page, <strong>and</strong> replace the existing contents of that section<br />

with the value of myString. Since this involves a little bit of new <strong>jQuery</strong>, let's look<br />

at it closely.<br />

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