DOMUtility

License

MIT-style license.

Author

Takashi Mizohata bea.nosp@m.tak@nydd.nosp@m..org

Copyright

2008 [nydd](http://code.nydd.org/).

Code & Documentation

[nydd jslib](http://code.nydd.org/).

Inspiration

Everything is worthwhile / if the soul isn’t small.

Summary
DOMUtilityMIT-style license.
Functions
isDOMNodeDetetect DOM Element or not.
removeAllChildrenRemove all child nodes.
addCSSAdd css safely.
addCSSHoverShortcut for addCSS with css name “hover”
removeCSS[FIX ME] It there are multiple name added on the element, it should remove everything.
removeCSSHoverShortcut for removeCSS with css name “hover”
defer[FIX ME] not completely done yet.
waitUntilVariableFires function after the given name is ready on global scope.
waitUntilImgLoadedFires function after the given DOM Element <img> is ready.
findInUpstreamFind a DOMNode on upstream side (parent side) by the given criteria.

Functions

isDOMNode

DOMUtility.isDOMNode = function (obj)

Detetect DOM Element or not.  It is using object detection methodology based on [DOM Core Level 1](http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html#method-getElementsByTagName).  If you happen to implement getElementsByTagName() on your object, it will gives you a wrong return.

Type

static

Parameters

objinstance of DOM Element

Examples

if (DOMUtility.isDOMNode(document)) ...

Retruns

(Boolean)

removeAllChildren

DOMUtility.removeAllChildren = function (parent)

Remove all child nodes.  It will not take care of garbage collection.  If some of child elements has Event handler and registered in Event chain, this may occur a memory leak.  Use with caution.

Type

static

Parameters

parentinstance of DOM Element

Examples

DOMUtility.removeAllChildren(document.body))

Retruns

undefined

addCSS

DOMUtility.addCSS = function (elm,
name,
regex)

Add css safely.  If the given css name is already on the element, it will not add more.

Type

static

Parameters

elminstance of DOM Element
name(String) name of css
regex[option] (Regex) just for optimization purpose.

Examples

DOMUtility.addCSS(document.body, ‘good’));

Retruns

(Boolean) true if this affects to elm.

addCSSHover

DOMUtility.addCSSHover = function (elm)

Shortcut for addCSS with css name “hover”

Type

static

Parameters

elminstance of DOM Element

Examples

DOMUtility.addCSSHover(document.body, ‘good’));

Retruns

undefined

removeCSS

DOMUtility.removeCSS = function (elm,
name,
regex)

[FIX ME] It there are multiple name added on the element, it should remove everything.  Remove css safely.

Type

static

Parameters

elminstance of DOM Element
name(String) name of css
regex[option] (Regex) just for optimization purpose.

Examples

DOMUtility.removeCSS(document.body, ‘good’));

Requires

Array.prototype.vacuum().  See Array

Retruns

(Boolean) true if this affects element.

removeCSSHover

DOMUtility.removeCSSHover = function (elm)

Shortcut for removeCSS with css name “hover”

Type

static

Parameters

elminstance of DOM Element

Examples

DOMUtility.removeCSSHover(document.body, ‘good’));

Retruns

undefined

defer

DOMUtility.defer = function (targets,
func)

[FIX ME] not completely done yet.  Fires function after the given element(s) is ready.  It only works with <img> and String.  Maybe I’d like to work with Script elements as well.

Type

static

Parameters

targetsjQuery object, or String
func(Function) be fired when all targets are ready.

Examples

DOMUtility.defer($(‘img.logo’), function(){alert(‘done’)});

Retruns

(Boolean) true, if it sets correctly; Otherwise false.

waitUntilVariable

DOMUtility.waitUntilVariable = function (word,
callback)

Fires function after the given name is ready on global scope.

Type

static

Parameters

word(String) expected variable name
callback(Function) be fired when word is ready.

Examples

DOMUtility.waitUntilVariable(‘myvar’, function(){alert(‘done’)});

Retruns

undefined

waitUntilImgLoaded

DOMUtility.waitUntilImgLoaded = function (elmImg,
callback)

Fires function after the given DOM Element <img> is ready.

Type

static

Parameters

elmImg(DOM Element) image element
callback(Function) be fired when word is ready.

Examples

DOMUtility.waitUntilImgLoaded(document.getElementById(‘logo’), function(){alert(‘done’)});

Retruns

undefined

findInUpstream

DOMUtility.findInUpstream = function (dom,
obj)

Find a DOMNode on upstream side (parent side) by the given criteria.  If nothing hits, will return null.

Type

static

Parameters

domDOM Element
objobject represents criteria.

Examples

hit = DOMUtility.findInUpstream(dom_child, {‘nodeName’: ‘address’})

Retruns

(DOMNode) or null.

DOMUtility.isDOMNode = function (obj)
Detetect DOM Element or not.
DOMUtility.removeAllChildren = function (parent)
Remove all child nodes.
DOMUtility.addCSS = function (elm,
name,
regex)
Add css safely.
DOMUtility.addCSSHover = function (elm)
Shortcut for addCSS with css name “hover”
DOMUtility.removeCSS = function (elm,
name,
regex)
[FIX ME] It there are multiple name added on the element, it should remove everything.
DOMUtility.removeCSSHover = function (elm)
Shortcut for removeCSS with css name “hover”
DOMUtility.defer = function (targets,
func)
[FIX ME] not completely done yet.
DOMUtility.waitUntilVariable = function (word,
callback)
Fires function after the given name is ready on global scope.
DOMUtility.waitUntilImgLoaded = function (elmImg,
callback)
Fires function after the given DOM Element img is ready.
DOMUtility.findInUpstream = function (dom,
obj)
Find a DOMNode on upstream side (parent side) by the given criteria.
MIT-style license.
Close