Overview
SLAB is a cross-browser Javascript library that implements ECMAScript and DOM fixes and enhancements to promote standards-based, browser-agnostic web-development.
SLAB upgrades the browser scripting environment to support the following features:
- New
Array
methods and generics:every, filter, forEach, map, some
1 Function.prototype.bind()
- binds a function to an object 2Object.defineProperty()
- specifies getter and/or setter functions for an object property 3- DOM prototypes -
HTMLElement
, etc element.classList
- provides methods to manipulate tokens in theclass
attributedocument|element.querySelector()
andquerySelectorAll()
- get elements by CSS selector- W3C DOM Event model
DOMContentLoaded
andprogress
4 events during document loadDOMNodeInserted
andDOMNodeRemoved
events- ... and more.
- Array generics are non-standard
- bind() only accepts one argument - the target of the bind
- There are limitations for getter/setter functions on browsers that don't have
object.__defineGetter__
, etc (mainly IE 6 & 7). - The
progress
event is non-standard ondocument
objects
Example
A simple SLAB enabled web-page could look like this
<html> <head> <!-- SLAB should be included before other scripts --> <script src="http://dist.meekostuff.net/SLAB/0.7-default/SLAB.js"></script>1,2 <script> /* * ECMAScript enhancements are available * DOM Prototypes are available * Custom DOM methods should be implemented before initializing DOM system */ HTMLFormElement.prototype.encode = function() { var txt = ""; Array.forEach(this.elements, function(el) { if (el.name) txt += el.name + ":" + encodeURIComponent(el.value) + "\n"; }); return txt; } /* * Initialize the DOM system */ Meeko.stuff.domSystem.initialize(); 3 /* * Standard DOM interfaces now available on window and document, * and are (almost always) automatically available on elements */ var onSubmit = function(event) { var form = event.target; var rc = confirm("Submit this data now?\n" + form.encode()); if (!rc) event.preventDefault(); } document.addEventListener("submit", onSubmit, false); </script> </head> <body> <form action="" method="GET"> <label>First name <input type="text" name="firstName" /></label><br /> <label>Last name <input type="text" name="lastName" /></label><br /> <input type="submit" /> </form> </body> </html>Notes:
- You can also link to a specific release or snapshot of the library.
- You could also download the library to your server and include it from there.
- DOM System initialization is triggered when the body element enters the document (if manual initialization hasn't occurred before).
This example adds an encode() method to all form elements and adds an event-listener for submit-events that requests user-confirmation of form-submission.
Warnings
Just-in-time Element binding
SLAB doesn't apply fixes and enhancements to elements at document load, which would potentially involve every element in the document.
Instead it attempts to guarantee that elements are fixed before your code references them.
This is done by over-riding DOM methods and properties for the accessing of elements, such as
document.getElementById()
,
document|element.querySelectorAll()
and
element.children
.
The following accessors are NOT supported:
document.getElementsByTagName()
element.childNodes
element.firstChild
element.lastChild
element.previousSibling
element.nextSibling
element.parentNode
Some accessors, such as
document|element.querySelectorAll()
and
element.children
,
return NodeList or HTMLCollection objects.
Elements in these collections must be accessed with the item()
method.
e.g. element.children.item(index)
.
SLAB does NOT support the commonly used array-notation,
and, where possible, prevents access using array notation so that,
element.children[index]
, for example would return null.
Generic Array methods filter, forEach, every, some
are provided
that can accept modified element collections.
No inline event handlers
SLAB does NOT support inline event handlers, e.g. element.onclick = function() { ... }
.
Features
SLAB upgrades the browser scripting environment with support for a subset of the APIs defined in the following standards:
- ECMAScript 5
- DOM Level 3 - Events
- Selectors API
- DOM Level 2 - Traversal and Range
- XMLHttpRequest
- HTML5
Licensing
SLAB is available under the Creative-Commons Attribution, No-Derivatives license. If you aren't sure whether that applies to your intended usage then contact me for specific approval. The following list are guides for some specific scenarios:
- you can use the library in any web-page by including a script element linking to the original file on the meekostuff.net domain
- you can also copy the library to the domain of the web-page and link to it there
- you can copy the library to another domain for the purpose of redistribution provided you acknowledge that it is the SLAB library and that you provide a link to the SLAB project page at meekostuff.net.
Long-term licensing is yet to be decided - please check the license conditions of new releases before use.