Javascript
There are various ways of doing this... and the Drupal API lists the following:
drupal_add_js('misc/collapse.js');
drupal_add_js('misc/collapse.js', 'file');
drupal_add_js('jQuery(document).ready(function () { alert("Hello!"); });', 'inline');
drupal_add_js('jQuery(document).ready(function () {…
Common problem when trying to get code to validate via W3C, just wrap it in CDATA tags…
<script type="text/javascript">
/*<![CDATA[*/
// place javascript here i.e.
function myFunction () {
// do something
}
/*]]>*/…
One thing you need to be careful of, and has caught me out a few times when jumping from PHP to Javascript, is ‘new lines’….
In JavaScript a new line suggests the end of a statement and the start of a new one… Where in PHP, the line can continue over several lines and doesn’t stop until you add…
In the words of Sitepoints Kevin Yank….
Your variable names can comprise almost any combination of letters and numbers, though no spaces are allowed.
Most punctuation and symbols have special meaning inside JavaScript, so the dollar sign ($) and the underscore (_) are the only non-alphanumeric…
To create an array in JavaScript is pretty straight forward, just declare the variable name equal to empty square brackets, like this:
var rack = [];
To add variables, you just specify the index you want to add the data to, like this:
rack[0] = "First";
rack[1] = "Second";
rack[2] = 3;…
The typical format for a ‘for loop’ to run through the list items in an array is as follows…
var myArray = [1, 2, 3, 5, 8, 13, 21, 34];
for (var i = 0; i < myArray.length; i++) {
//myArray[i] becomes the variable run inside the loop
}
But you’ll notice that every time the loop is run,…
Basic list of example code you can copy & paste…
var myElement = document.getElementById("elementId");
var listItems = document.getElementsByTagName("li");
for (var i = 0, ii = listItems.length; i < ii; i++) {
alert(listItems[i].nodeName);
}
var lists = document.…
Quick Reference guide of dot operators:
.length
// counts the number of items in an array
.nodeName
// reference the name of the node, i.e. p for p tag, etc.
.parentNode
// references the parent on an element node
.childNodes
// references the elements of children. i.e. the parent can be a…
The getElementsByClassName() is already present in some modern browsers, but this code from Kevin Yank at Sitepoint just adds it for those browsers that don’t already have it….
function getElementsByClassName(className) {
// get all elements in the document
if (document.all) {…
Ever had it where your stylesheet contains code that doesn’t validate with the W3C standards…. Yet there’s not an easy way round it? For example ‘opacity’…..
This code adds an additional stylesheet after the page has loaded, allowing you to pop the in-valid code whilst achieving a valid mark from…