Extend
Download

Extend by Geoff Lankow is licensed under a Creative Commons Attribution 3.0 License.
Extend is a library to provide utility functions to Javascript. Where possible the functions are added in a transparent way if they don't exist. These examples aren't brilliant, but they're here if you want some idea what's going on - as large chunks of my other code rely on Extend.
Additional Functions
Extend provides the functions Array.indexOf and String.trim, which are not available in some browsers. It also provides JSON.stringify, but not JSON.parse.
_loadList
an array of functions to be called at as soon as the page is loaded. Example:
_loadList.push(function() {
alert('page loaded');
});
$
synonym for document.getElementById.
$c(tag)
removes all elements from the specified tag.
$e(tag, newTag, [text], [attributes])
creates a new tag of type newTag, with HTML attributes, and containing text.
$e($('test1'), // we're going to append our new tag to the test area below
'div', // it'll be a div tag
'this is an example', // with this text
{"class":"test","style":"color: #c00"}); // and the class and style attributes
$ea(tag, argArray)
calls $e for each item in argArray - items are arrays of arguments for $e.
$e($('test2'), // we're going to append our new tag to the test area below
[
[null, 'this is an example'], // a text node
['br'] // a line break
]);
$cna, $cnr and $cnc
Implementations of the classList functions for browsers that don't support them.
$cna(tag, name) // same as tag.classList.add(name) $cnr(tag, name) // same as tag.classList.remove(name) $cnc(tag, name) // same as tag.classList.contains(name)
$up(tag, nodeName)
finds the closest ancestor of tag with nodeName, or null if no such ancestor is found.