4.6. The Browser Object

ASP.NET has extensive information about various browsers. It provides a whole system for analyzing headers sent in by a browser and producing a complex Browser 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 specifies 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-25 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.

Example 4-25. Working with the Browser object
<%@ Page Language="C#" Debug="true" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Browser object</title>

    <script type="text/javascript">

        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()));

        }

function someMethod(argument) {
            if(argument === null) {
                alert("null");
            }
        }


    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>

        <textarea id="TraceConsole" cols="512" rows="10"
         style="width:100%;border:solid black 1px"></textarea>

    </div>
    </form>
</body>
</html>

The different values returned by the Sys.Browser properties are shown in Figure 4-13.

Figure 4-13. Figure 4-13

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset