Generic site object in JavaScript

Here's a generic wrapper that I use often while developing a new site:

function Site() {

    var title = document.title;
    var url = location.href;
    var host = location.host;
    var ua = navigator.userAgent;
    var uaVer = parseFloat(navigator.appVersion);
    
    this.getBrowser = function() {
        
 var browser;
         
 if(/Firefox/.test(ua)) {
     browser = 'firefox';
 } else if(/MSIE/.test(ua)) {
     browser = 'ie';
 } else if(/Opera/.test(ua)) {
     browser = 'opera';
 } else if(/\d\.\d\sSafari/.test(ua)) {
     browser = 'safari';
 } else if(/Chrome/.test(ua)) {
     browser = 'chrome';
 } else {
     browser = 'unknown';
 }
 
 return browser;
 
    };
    
    this.getIEVersion = function() {
    
        var browser = this.getBrowser();
 
 if(browser == 'ie') {
 
     if(ua.indexOf('MSIE 7') != -1) {
       return (browser + '7');
     } else if(ua.indexOf('MSIE 8') != -1) {
       return (browser + '8');
     } else {
       return (browser + '6');
     }
 
 } else {
     return;
 }
    
    };
}

Obviously you can add more generic methods and properties to this wrapper. Notice, however, how the first variables act more like constants than variables (the keyword const is not widely supported) and are available only inside the object that hosts them (in fact, they quite resemble the behavior of private members).

Leave a Reply

Note: Only a member of this blog may post a comment.