ASP.NET 2.0 Unleashed
Using the ClientScriptManager Class
The ClientScriptManager class contains the main application programming interface for working with JavaScript. You'll make heavy use of this class whenever you add JavaScript to your custom controls. The ClientScriptManager class supports the following methods for adding JavaScript to a page:
Notice that there are two methods for rendering a JavaScript script in the body of a page: RegisterClientScriptBlock() and RegisterStartupScript(). The only difference between these methods is the location where they render the JavaScript. The location of a JavaScript script in a page matters because you cannot refer to an HTML element in JavaScript unless the script is located after the element. If you use the RegisterStartupScript() method, then you know that all the HTML elements in the body of the server-side <form> tag have been created. All the methods listed here were designed so that you can safely call them more than once. Because you might have multiple instances of the same control in the same page, you don't want to add duplicate instances of the same script to a page. For example, if you call the RegisterClientScriptInclude() method more than once, then only one JavaScript include is added to the page. You can detect whether or not a script has already been registered in a page by using one of the following methods:
Detecting Browser Capabilities
After you have entered the messy universe of JavaScript, you must handle the frustrating incompatibilities between different web browsers. For example, you don't want to call the showModalDialog() or addEventListener() method on a browser that doesn't support it. You can detect browser capabilities either on the client side or the server side. On the client side, you can perform feature detection in your JavaScript scripts to check whether particular methods are supported by a browser. For example, Internet Explorer and Firefox use different methods for adding an event handler. Internet Explorer uses the attachEvent() method and Firefox uses the (more standards-compliant) addEventListener() method. The following script correctly adds a load event handler in the case of both browsers: if (window.addEventListener) window.addEventListener('load', doSomething, false); else window.attachEvent('onload', doSomething);
When you request a page that contains this script with Internet Explorer, calling window.addEventListener returns a value equivalent to false and the window.attachEvent() method is used. When you request a page that contains this script with Firefox or Opera, on the other hand, the window.addEventListener() method is called. On the server side, you can use the properties of the HttpBrowserCapabilities class to detect the features of the browser being used to request a page. This class has a huge number of properties (too many to list here). However, here are some of the more useful properties that you can detect:
The HttpBrowserCapabilities object is exposed through the Request object. You use Request.Browser to get a reference to the HttpBrowserCapabilities object. For example, you can use the following code to execute a subroutine only when the requesting browser is Internet Explorer version 5.0 or greater: If Request.Browser.Browser = "IE" And Request.Browser.MajorVersion >= 5 Then doSomething() End If Behind the scenes, the HttpBrowserCapabilities object uses the User-Agent header sent by a browser to determine the browser's capabilities. A database of browser capabilities is stored in a set of XML files located in the following folder: \WINDOWS\Microsoft.NET\Framework\[version]\CONFIG\Browsers The information reported back by these properties is only as accurate as the information stored in these XML files. |
Категории