This is an old revision of this page, as edited by Mike Dillon(talk | contribs) at 00:46, 23 October 2006(pass "elem" as first argument to attribute callbacks). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.Revision as of 00:46, 23 October 2006 by Mike Dillon(talk | contribs)(pass "elem" as first argument to attribute callbacks)
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// <pre><nowiki>functioncreateEasyDomFunction(name){varstringType=typeof'';varfunctionType=typeoffunction(){};// Detects if the first element is a hash of attributes and if so,// uses it to set attributes on the DOM node//// Returns the number of elements processed to let the caller know// how many of the arguments to skipvarprocessAttributes=function(elem,args){if(args.length==0){return0;}// Detect string argumentsvarfirstArgType=typeofargs[0];if(firstArgType==stringType){return0;}// Detect function argumentsif(firstArgType==functionType){return0;}// Detect DOM nodesif(typeofargs[0].nodeType!='undefined'){return0;}// Assume that we got a hash of attributes as the first arg...varattrs=args[0];for(varattrNameinattrs){varattrValue=attrs[attrName];// Invoke function callbacks and use their result as the valueif(typeofattrValue==functionType){attrValue=attrValue(elem);}// Skip null valuesif(attrValue==null){continue;}// Set the attributeelem.setAttribute(attrName,attrValue);}// Return the number of arguments processedreturn1;};// Return the function that creates new DOM elementsreturnfunction(){varelem=document.createElement(name);// Process attribute hash, if any and skip the argument count returnedvarfirstChild=processAttributes(elem,arguments);// Process the remaining children, if anyfor(vari=firstChild;i<arguments.length;i++){varchild=arguments[i];if(typeofchild==stringType){child=document.createTextNode(child);}elem.appendChild(child);}returnelem;};}vareasyDomTags=["script","p","a","div","span","ul","ol","li","img","hr","br","em","strong","table","tbody","tr","th","td","input","h1","h2","h3","h4","h5"];vareasyDom={};for(variineasyDomTags){easyDom[easyDomTags[i]]=createEasyDomFunction(easyDomTags[i]);}// Namespace pollutionvareasydom=easyDom;vareasyDOM=easyDom;// </nowiki></pre>