User:Jesdisciple/global.js

window._isNew = true; window.onload = function (e){ if(window._isNew){ // Avoid infinite event-chaining loops. window._isNew = false; if(window.location.hostname == 'javascript.wikia.com'){ var editor = document.getElementById('editor'); if(editor){ if(BrowserDetect.browser == 'Explorer'){ var ARR_ACTIVEX = ["MSXML4.DOMDocument", // http://www.webreference.com/programming/javascript/domwrapper/index.html "MSXML3.DOMDocument", "MSXML2.DOMDocument", "MSXML.DOMDocument", "Microsoft.XmlDom"]; window.STR_ACTIVEX = "", window.ACTIVEX; //iterate through strings to determine which one to use for (var i = 0; i < ARR_ACTIVEX.length && !window.STR_ACTIVEX; i++) { //set up try...catch block for trial and error //of strings try { //try to create the object, it will cause an                            //error if it doesn't work var objXML = new ActiveXObject(ARR_ACTIVEX[i]); //if it gets to this point, the string worked, //so save it                           window.STR_ACTIVEX = ARR_ACTIVEX[i]; } catch (e) { } //End: try } //End: for //if we didn't find the string, send an error if(STR_ACTIVEX){ var error = "MSXML is not found on your computer. Try either getting support from Microsoft or using a browser other than Internet Explorer." }               }                var row = editor.rows[1]; if(error){ row.cells[0].innerHTML = error; }else{ editor.rows[0].cells[0].style.backgroundColor = row.style.borderColor = '#c9c9ff'; row.style.backgroundColor = '#f3f3ff'; var cell1 = row.cells[0]; cell1.width = cell1.style.width = "50%"; cell1.style.textAlign = 'right'; while(cell1.hasChildNodes){ cell1.removeChild(cell1.lastChild); }                   var input = document.createElement('textarea'); input.width = input.style.width = '100%'; input.value = '\n' + '   \n' + '       Titled Document \n' + '   \n' + '   \n' + '       Hello, world! \n' + '   \n' + ' \n'; cell1.appendChild(input); var button = document.createElement('button'); button.value = button.innerText = button.textContent = 'Render'; button.onclick = function { var dom = parseXml(input.value); if(dom.parseError && dom.parseError.errorCode == 0){ editor.rows[0].cells[0].style.backgroundColor = row.style.borderColor = '#c9c9ff'; row.style.backgroundColor = '#f3f3ff'; var onload = window.onload; var scripts = dom.getElementsByTagName('script'); for(var i = 0; i < scripts.length; i++){ eval(scripts[i].firstChild.nodeValue); }                           cell2.innerHTML = dom.getElementsByTagName('body')[0].xml; if(window.onload !== onload){ window.onload(e); }                       }else{ editor.rows[0].cells[0].style.backgroundColor = row.style.borderColor = '#ffc9c9'; row.style.backgroundColor = '#fff3f3'; cell2.innerHTML = ' ' + dom.parseError.reason + ' '; }                   };                    cell1.appendChild(button); var cell2 = row.insertCell(1); }           }        }    } }; function parseXml(xmlString){ var dom; switch(BrowserDetect.browser){ // http://www.quirksmode.org/js/detect.html case 'Explorer': // http://www.webreference.com/programming/javascript/domwrapper/index.html dom = new ActiveXObject(window.STR_ACTIVEX); dom.onreadystatechange = function{ if(this.readyState == 4){ this.onload; }           };            dom.loadXML(xmlString); case 'Safari': if(XMLHttpRequest){ // http://web-graphics.com/mtarchive/001606.php (I think they're wrong about being wrong.) var ajax = new XMLHttpRequest; ajax.onreadystatechange = function{ if(this.readyState == 4){ this.onload; }               }                ajax.onload = function{ dom = this.responseXML; };               ajax.open('GET', 'data:text/xml;charset=utf-8,' + xmlString, true); ajax.send(null); }       default: // Firefox, Opera, etc.            Node.prototype.__defineGetter__("xml", function {                    //create a new XMLSerializer                    var objXMLSerializer = new XMLSerializer;                    //get the XML string                    var strXML = objXMLSerializer.serializeToString(this);                    //return the XML string                    return strXML;                }); //create new document from string dom = (new DOMParser).parseFromString(xmlString, "application/xhtml+xml"); if (!dom.documentElement || dom.documentElement.tagName == "parsererror"){ dom.parseError = {errorCode: -9999999, reason: dom.documentElement.nodeValue, srcText: dom.documentElement.lastChild}; }   }    return dom; } Document.prototype.parseError = {errorCode: 0}; var BrowserDetect = { // http://www.quirksmode.org/js/detect.html init: function { this.browser = this.searchString(this.dataBrowser) || "An unknown browser"; this.version = this.searchVersion(navigator.userAgent) || this.searchVersion(navigator.appVersion) || "an unknown version"; this.OS = this.searchString(this.dataOS) || "an unknown OS"; },   searchString: function(data){ for (var i=0;i<data.length;i++){ var dataString = data[i].string; var dataProp = data[i].prop; this.versionSearchString = data[i].versionSearch || data[i].identity; if(dataString){ if (dataString.indexOf(data[i].subString) != -1){ return data[i].identity; }           }else if(dataProp){ return data[i].identity; }       }    },    searchVersion: function (dataString) { var index = dataString.indexOf(this.versionSearchString); if (index == -1) return; return parseFloat(dataString.substring(index+this.versionSearchString.length+1)); },   dataBrowser: [ {           string: navigator.userAgent, subString: "Chrome", identity: "Chrome" },       {     string: navigator.userAgent, subString: "OmniWeb", versionSearch: "OmniWeb/", identity: "OmniWeb" },       {            string: navigator.vendor, subString: "Apple", identity: "Safari" },       {            prop: window.opera, identity: "Opera" },       {            string: navigator.vendor, subString: "iCab", identity: "iCab" },       {            string: navigator.vendor, subString: "KDE", identity: "Konqueror" },       {            string: navigator.userAgent, subString: "Firefox", identity: "Firefox" },       {            string: navigator.vendor, subString: "Camino", identity: "Camino" },       {        // for newer Netscapes (6+) string: navigator.userAgent, subString: "Netscape", identity: "Netscape" },       {            string: navigator.userAgent, subString: "MSIE", identity: "Explorer", versionSearch: "MSIE" },       {            string: navigator.userAgent, subString: "Gecko", identity: "Mozilla", versionSearch: "rv" },       {         // for older Netscapes (4-) string: navigator.userAgent, subString: "Mozilla", identity: "Netscape", versionSearch: "Mozilla" }   ],    dataOS: [ {           string: navigator.platform, subString: "Win", identity: "Windows" },       {            string: navigator.platform, subString: "Mac", identity: "Mac" },       {            string: navigator.platform, subString: "Linux", identity: "Linux" }   ]

}; BrowserDetect.init;