DOM reference example | WebReference

DOM reference example

shades id="myImage"
syntax if (document.layers) if (document.all) else
mElement('mainLayer') document.layers['mainLayer'] document.all.mainLayer document.getElementById('mainLayer')
mElement('mainLayer','DoC') document.layers['mainLayer'].document document.all.mainLayer document.getElementById('mainLayer')
mElement('myImage','iMageZ') document.images['myImage'] document.all.myImage document.getElementById('myImage')
mElement('myImage2','mainLayer','iMageZ') document.layers['mainLayer'].document.images['myImage2'] document.all.myImage2 document.getElementById('myImage2')
mElement('myForm','mainLayer','ForMz') document.layers['mainLayer'].document.forms['myForm'] document.all.myForm document.getElementById('myForm')
mElement('secondLayer','mainLayer') document.layers['mainLayer'].document.layers['secondLayer'] document.all.secondLayer document.getElementById('secondLayer')
This is 'mainLayer'
shades id="myImage2"
This is 'myForm'
This is 'secondLayer'
mElement() source code:
function mElement(elementName) {
	if (document.layers) {
	//we want 1 object (not nested)
		//simple layer reference
		if (mElement.arguments.length == 1) {
			var cEl = eval("document.layers['" + elementName + "']");
		
		//refer document in layer: document.layers[layername].document
		} else if (mElement.arguments[1] == 'DoC') {// use 'strange' spelling to minimise conflicts with existing objects
			var cEl = eval("document.layers['" + elementName + "'].document");
		//refer an image: document.images[imageName]
		} else if (mElement.arguments[1] == 'iMageZ') {
			var cEl = eval("document.images['" + elementName + "']");
		
		//nested objects
		} else if (mElement.arguments.length>1) {
			var cEl = "document.layers['" + mElement.arguments[1] + "']";//start the string
			for (var i=2;i<mElement.arguments.length;i++) {//leave the first argument, because that's the one you're after, the second because you used that one to start the string
				if ( (mElement.arguments[i] != 'DoC') && (mElement.arguments[i] != 'iMageZ') && (mElement.arguments[i] != 'ForMz') ) {
					//alert(mElement.arguments[i]);
					cEl = cEl + ".document.layers['" + mElement.arguments[i] + "']";//get the next layer
				}
			}
			if (mElement.arguments[mElement.arguments.length-1] == 'iMageZ')  {
				cEl = cEl + ".document.images['" + elementName + "']";//in case you need the images in the layer rather than the layer or doc
			} else if (mElement.arguments[mElement.arguments.length-1] == 'ForMz')  {
				cEl = cEl + ".document.forms['" + elementName + "']";//in case you need the form in the layer rather than the layer or doc
			} else {
				cEl = cEl + ".document.layers['" + mElement.arguments[0] + "']";//add first element (the one you're after)
				if (mElement.arguments[mElement.arguments.length-1] == 'DoC') {
				cEl = cEl + ".document";//in case you need the document in the layer rather than the layer itself
				}
			} 
			cEl = eval(cEl);//wrap up and make string into an object
		} else {
			var cEl = eval("document.layers['" + mElement.arguments[1] + "'].document." + elementName);
		}
	
	//old IE browsers:
	} else if (document.all) {
		var cEl = eval('document.all.' + elementName);
	
	//W3C standard:
	} else {
		var cEl = document.getElementById(elementName);
	}
	return cEl;
}