How do I reference <HTML> element via Javascript?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Arodicus
    New Member
    • May 2007
    • 12

    #1

    How do I reference <HTML> element via Javascript?

    This is bugging me: how do I reference the topmost node (HTML) within a document? I'd like to set a class on it, which specifies various browser/os/versions so that several browser/os/version-specific css bugs may be addressed without coding. eg: <HTML class="_IE _IE7 _Win _IE7Win">

    The closest I've come is document.body.p arentNode (line 22, below), but this of course requires that my script be executed from within body... I'd prefer to execute from within HEAD, while the document is still loading.

    The code is below. In a nutshell, it does a quick browser detect, writes the findings out to several classes, then erases itself from memory as it is no longer needed. Also, I think this is the only detection script out there that correctly associates Safari Build Versions with the corrresponding Safari Release Version, since there's no way to get the actualy release version from within Safari (way to go, Apple!).



    Code:
    (function(){
    	var maj = Math.floor;
        var BrowserDetect = {
    	    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";
    		    
    		    if(this.browser == "Safari") {
    		        this.build = this.version
    		        this.version = this.searchSafariBuilds(this.version, this.dataSafariBuild)
    		    }
    		    // add special class for css
    		    var clientClass= "_"+this.browser;
    		    var clientVersionClass= "_"+this.browser+maj(this.version)
    		    var clientVersionOSClass= "_"+this.browser+maj(this.version)+this.OS
    		    var osClass= "_"+this.OS;		    
    		    
    		    myClasses = clientClass + " " + clientVersionClass + " " + osClass + " " + clientVersionOSClass;	    
    		    document.body.parentNode.className= myClasses; 
    	    },
    	    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));
    	    },
        	
        	searchSafariBuilds : function (vers, data) {
        	    /* Safari uses internal "build number" instead of a release number, 
        	    so this translates for us non-apple-employees.*/
        	    vers = Number(vers.toString().split(".",2).join("."))
        	    var releaseVersion = "Unknown";
                for (var i=0;i<data.length;i++)	{
        	        if(vers > data[i].Safari){ 
        	            releaseVersion = data[i].Version; 
        	        }
        	    }
        	    return releaseVersion;
        	},
        	
    	    dataBrowser: [
    		    { 	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: "FF"
    		    },
    		    {
    			    string: navigator.vendor,
    			    subString: "Camino",
    			    identity: "Camino"
    		    },
    		    {		// for newer Netscapes (6+)
    			    string: navigator.userAgent,
    			    subString: "Netscape",
    			    identity: "NS"
    		    },
    		    {
    			    string: navigator.userAgent,
    			    subString: "MSIE",
    			    identity: "IE",
    			    versionSearch: "MSIE"
    		    },
    		    {
    			    string: navigator.userAgent,
    			    subString: "Gecko",
    			    identity: "Moz",
    			    versionSearch: "rv"
    		    },
    		    { 		// for older Netscapes (4-)
    			    string: navigator.userAgent,
    			    subString: "Mozilla",
    			    identity: "NS",
    			    versionSearch: "Mozilla"
    		    }
    	    ],
    	    dataOS : [
    		    {
    			    string: navigator.platform,
    			    subString: "Win",
    			    identity: "Win"
    		    },
    		    {
    			    string: navigator.platform,
    			    subString: "Mac",
    			    identity: "Mac"
    		    },
    		    {
    			    string: navigator.platform,
    			    subString: "Linux",
    			    identity: "Linux"
    		    }
    	    ],
    	    
    	    dataSafariBuild :  [
    	    
                {Version: ">1.0",    Webkit: 0.0,   Safari: 0.0},
                {Version: "1.0",    Webkit: 85.0,   Safari: 85.0},
                {Version: "1.0.2",  Webkit: 85.7,   Safari: 85.7},
                {Version: "1.0.3",  Webkit: 85.8,   Safari: 85.8},
                {Version: "1.1",    Webkit: 100,    Safari: 100},
                {Version: "1.1.1",  Webkit: 100,    Safari: 100.1},
                {Version: "1.2.2",  Webkit: 125.2,  Safari: 125.7},
                {Version: "1.2.2",  Webkit: 125.2,  Safari: 125.8},
                {Version: "1.2.3",  Webkit: 125.4,  Safari: 125.9},
                {Version: "1.2.3",  Webkit: 125.5,  Safari: 125.9},
                {Version: "1.2.4",  Webkit: 125.5,  Safari: 125.11},
                {Version: "1.3",    Webkit: 312.1,  Safari: 312},
                {Version: "1.3.1",  Webkit: 312.5,  Safari: 312.3},
                {Version: "1.3.2",  Webkit: 312.8,  Safari: 312.5},
                {Version: "1.3.2",  Webkit: 312.8,  Safari: 312.6},
                {Version: "2.0",    Webkit: 400,    Safari: 400},
                {Version: "2.0.1",  Webkit: 412.7,  Safari: 412.5},
                {Version: "2.0.2",  Webkit: 416.11, Safari: 416.12},
                {Version: "2.0.2",  Webkit: 416.12, Safari: 416.13},
                {Version: "2.0.3",  Webkit: 417.9,  Safari: 417.8},
                {Version: "2.0.3",  Webkit: 417.9,  Safari: 417.9},
                {Version: "2.0.3",  Webkit: 418,    Safari: 417.9},
                {Version: "2.0.4",  Webkit: 418.8,  Safari: 419.3},
                {Version: "2.0.4",  Webkit: 418.9,  Safari: 419.3},
                {Version: "2.0.4",  Webkit: 419,    Safari: 419.3},
                {Version: "2.0.4",  Webkit: 419.3,  Safari: 419.3},
                {Version: "3.0",    Webkit: 500,    Safari: 500},
                {Version: "3.0.3",  Webkit: 522,    Safari: 522},
                {Version: "3.0.4",  Webkit: 523.10, Safari: 523.1},
                {Version: "4.0",    Webkit: 600.0,  Safari: 600.0}
    	    ]
        }
        BrowserDetect.init()
    
    })();

    (Original code is based off of http://www.quirksmode.org/js/detect.html, with my additions being the self-termination and Safari version detection.)
  • mrhoo
    Contributor
    • Jun 2006
    • 428

    #2
    document.docume ntElement refers to the html root element.


    Your browser sniffer could lose some weight.

    [CODE=javascript]navigator.whozi t=function () {
    var N= navigator.appNa me;
    var ua= navigator.userA gent, M;
    if (/opera/i.test(ua)) {
    M= ua.match(/(opera)\/? *(\.?\d+(\.\d+) *)/i);
    }
    else {
    M= ua.match(/(applewebkit|fi refox|seamonkey |netscape)\/ *(\.?\d+(\.\d+) *)/i);
    if (M) {
    if (/safari/i.test(ua)) M[1]= "Safari";
    var tem= ua.match(/version\/(\.?\d+(\.\d+)? )/i);
    if (tem) M[2]= tem[1];
    }
    else M= ua.match(/ms(ie) *(\d+(\.\d+)*)/i);
    }
    M= M ? [M[1], M[2]] : [N, navigator.appVe rsion];
    M[0]= M[0].length > 2 ? M[0].capit(true) : M[0].toUpperCase();
    return M;
    }[/CODE]
    //test case
    alert(navigator .whozit())

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Originally posted by Arodicus
      In a nutshell, it does a quick browser detect, writes the findings out to several classes, then erases itself from memory as it is no longer needed.
      ...[snip]
      (Original code is based off of http://www.quirksmode.org/js/detect.html, with my additions being the self-termination and Safari version detection.)
      I'm just wondering what purpose this serves? Is it just a test project?

      Comment

      • Arodicus
        New Member
        • May 2007
        • 12

        #4
        It started out as an experiment but now we're looking into using it to help address numerous VERY specific browser bugs; for instance turning off certain features in safari mac 2.0.0.6 but leaving them on in 2.0 and 3.0.

        I realize the code is a little bloated but part of the requirement is that a team of people with only minimal coding skills will be taking over the project eventually; therefore it's clearer to keep everything in data tables rather than using RegExps, and to use CSS flags in the body of the beast rather than rely on inline calculations.

        Comment

        Working...