Xajax (any): Tips and Tricks: Detecting Support






Xajax (any): Tips and Tricks: Detecting Support

Here is some html/javascript code you can use to detect whether or not the user's browser supports cookies, javascript, and ajax and then display a message if it doesn't.

Helper functions for detecting cookie and Ajax support:

<script type="text/javascript">
<!--
    // Cookie handling 
    var Cookie =
    {
        read: function (name)
        {
            var arrCookies = document.cookie.split ('; ');
            for (var i=0; i<arrCookies.length; i++)
            {
                var arrCookie = arrCookies[i].split ('=');
                
                if (arrCookie[0] == name)
                {
                    return decodeURIComponent (arrCookie[1]);
                }
            }
            return false;
        },
    
        write: function (name, value, expires, path)
        {
            if (expires)
            {
                var date = new Date ();
                date.setTime (date.getTime () + (((((expires * 24) * 60) * 60) * 1000)));
                expires = '; expires=' + date.toGMTString ();
            }
            else expires = '';
    
            if (!path) path = '/';
    
            document.cookie = name+'='+encodeURIComponent (value)+expires+'; path='+path;
        },
    
        remove: function (name)
        {
            this.write (name, '', -1);
        }
    }
    
    // Detects if can set a cookie in the browser
    function browserSupportsCookies()
    {
        Cookie.write('cookiesEnabled', 1);
        var boolCookiesEnabled = Cookie.read('cookiesEnabled');
        Cookie.remove('cookiesEnabled');
        if (boolCookiesEnabled != 1)
        {
            return false;
        }
        return true;
    }
    
    // Detects if the browser supports Ajax 
    function browserSupportsAjax()
    {
        if (typeof XMLHttpRequest == "undefined" && typeof ActiveXObject == "undefined" && window.createRequest == "undefined")
        {
            return false;
        }
        return true
    }
    
    // Detects if the browser can use ActiveX if necessary
    function ActiveXEnabledOrUnnecessary ()
    {
        if (typeof ActiveXObject != "undefined")
        {
            var xhr = null;
            try{
                xhr=new ActiveXObject("Msxml2.XMLHTTP");
            }catch (e){
                try{
                    xhr=new ActiveXObject("Microsoft.XMLHTTP");
                }catch (e2){
                    try{
                        xhr=new ActiveXObject("Msxml2.XMLHTTP.4.0");
                    }catch (e3){
                        xhr=null;
                    }
                }
            }
            if (xhr == null)
            {
                return false
            }
        }
        
        return true;
    }
-->
</script>

Display an error message to the user if they don't have the proper support:

<!-- Detect support for cookies and Ajax and display message if not -->        
<div id="supportError">
    <noscript>
        <div>JavaScript appears to be either disabled or unsupported by your browser.
        This web application requires JavaScript to function properly.
        Please enable JavaScript in your browser settings,
        or upgrade to a browser with JavaScript support and try again.</div>
    </noscript>
    
    <script type="text/javascript">
    <!--
    
    if (!browserSupportsCookies())
    {
        var msg = '<div>Cookies appear to be either disabled or unsupported by your browser. '
        msg += 'This web application requires Cookies to function properly. ';
        msg += 'Please enable Cookies in your browser settings ';
        msg += 'or upgrade to a browser with Cookie support and try again.</div>'
        
        document.write(msg);
    }
    
    if (!browserSupportsAjax())
    {
        var msg = '<div>Your browser does not appear to support Ajax technology. '
        msg += 'This web application requires Ajax to function properly. ';
        msg += 'Please upgrade to a browser with Ajax support and try again.</div>';
        
        document.write(msg);
    }
        
    if (!ActiveXEnabledOrUnnecessary())
    {
        var msg = '<div>ActiveX appears to be disabled in your browser. ';
        msg += 'This web application requires Ajax technology to function properly. ';
        msg += 'In Internet Explorer versions earlier than 7.0, Ajax is implemented using ActiveX. ';
        msg += 'Please enable ActiveX in your browser security settings ';
        msg += 'or upgrade to a browser with Ajax support and try again.</div>';
        
        document.write(msg);
    }    
    -->
    </script>
</div>