The Browser Object

The Browser Object

ASP.NET has an extensive set of information about various browsers. There is a whole system for analyzing the headers sent in by a browser and producing a complex Browser Capabilities object that you can use from the page to tailor the application for the user. The control adapter architecture is based on this configuration system being able to specify where custom renderings should be used for particular browsers.

The AJAX Library defines some basic browser definitions and determines which one is being used. JavaScript code can then easily check the browser object and draw conclusions about any unique requirements of the browser.

Listing 4-23 (Browser.aspx) displays the information currently stored in the Browser object. It also shows how you can use an indexer to set custom properties on the Browser object. Remember that you can do this for any object in JavaScript. This is a case where extensibility is easy and intuitive. The different values returned by the Sys.Browser properties are shown in Figure 4-7.

Listing 4-23
Image from book
<%@ Page Debug="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">
    function someMethod(argument) {
        if(argument === null) {
            alert("null");
        }
    }

    function pageLoad(sender, args) {        
        Sys.Debug.trace("name = " + Sys.Browser.name);
        Sys.Debug.trace("version = " + Sys.Browser.version.toString());
        Sys.Debug.trace("hasDebuggerStatement = " + 
Sys.Browser.hasDebuggerStatement.toString());
        if(Sys.Browser.agent === Sys.Browser.InternetExplorer) {
            Sys.Debug.trace("agent = Sys.Browser.InternetExplorer");
        }
        else if(Sys.Browser.agent === Sys.Browser.Firefox) {
            Sys.Debug.trace("agent = Sys.Browser.Firefox");
        }
        else if(Sys.Browser.agent === Sys.Browser.Safari) {
            Sys.Debug.trace("agent = Sys.Browser.Safari");
        }
        else if(Sys.Browser.agent === Sys.Browser.Opera) {
            Sys.Debug.trace("agent = Sys.Browser.Opera");
        }
        Sys.Browser.agent["supportsAudio"] = true;
        Sys.Debug.trace(String.format("{0} supportsAudio = {1}", Sys.Browser.name, 
Sys.Browser.agent["supportsAudio"].toString()));                
    }    
    </script>
</head>
<body>
    <form id="form1" runat="server">    
    <div>
    <asp:ScriptManager runat="server" ID="sm" EnableScriptGlobalization="true" />
    <textarea id="TraceConsole" cols="512" rows="10" style="width:100%;border:solid
black 1px"></textarea>
    </div>
    </form>
</body>
</html>
Image from book
Image from book
Figure 4-7