PDA

View Full Version : 3 handy DOM functions


mrhoo
05-31-2006, 08:05 PM
DOM detector:
function imp(wot, wch){
var DI= (typeof(document)=='object')?
document.implementation: false;

if(DI && DI.hasFeature) return !!(DI.hasFeature(wot,wch));
else return false;
}
eg: if(imp('html','1.0')){ // DOM enabled code
if(imp('Events','2.0')){// uses DOM 2 event model

Event handler assignment:
function addEvent(hoo,wot,fun){
if(hoo.addEventListener) hoo.addEventListener(wot,fun,null);
else if(hoo.attachEvent) hoo.attachEvent('on'+wot,fun);
else hoo['on'+wot]=fun;
}

eg: addEvent(self,'load',doSomething);
addEvent(mr('input'),'change',verifyInput);

This one verifies that the argument is an element or its id and returns the element:

function mr(hoo){
if(!hoo) return false;
if(typeof(hoo)== 'string') hoo= document.getElementById(hoo);
return (hoo && hoo.nodeType==1)? hoo: false;
}