MSDN Magazine - March 2009 - (Page 55) Likewise, each time the users disconnects, the network the Offline method will fire. XMLHttpRequest Time Outs The new XMLHttpRequest in Internet Explorer has a TimeOut property and an ontimeout event. This enables you to define XMLHttpRequests which will timeout and allow other requests to move ahead in the request queue.In the following code, I create a new XmlHttpRequest and set the timeout property and ontimeout event listener: function GetAReqeust() { var MyRequest = XMLHttpRequest(); MyRequest.ontmeout = TimeOutHere; MyRequest.open("GET","http://www.contoso.com/data.xml"); MyRequest.timeout = 2000; MyRequest.send(null); } function TimeOutHere() { alert("Request to Contoso timed out!"); } be so much larger in part because the data is not sent with each request to the server as it is with cookies. Plus, the localStorage never expires. In order to store and retrieve data from DOM storage, all you need is a key/value pair. The localStorage JavaScript class provides the methods setItem, getItem and removeItem to access all the features of DOM Storage: function Save() { localStorage.setItem("MyItem", $get("Text1").value); } function Load() { $get("Text1").value = localStorage.getItem("MyItem"); } This code performs the equivalent of a property get and set operation in C# or Visual Basic using DOM Storage as the backing store. First of all, the Save function stores the value of a TextBox to DOM Storage with a key name of MyItem. Then the Load function retrieves the data from DOM Storage by providing the same key name. Offline and Online Working Modes Like many Web developers, I’ve long been dreaming of the day when I could detect if the current user has a healthy connection to the Internet and provide an appropriate level of functionality. Now, Internet Explorer provides the status of the host PCs Internet connection with onLine property of window.navigator. While in the past, this property represented whether the user had chosen to work offline, it now indicates whether the current user has an established connection to the network. In addition to the onLine property, Internet Explorer has now two new callback handlers ononline and onoffline (see msdn.microsoft.com/library/cc304127 and msdn.microsoft.com/library/cc304126). The onoffline handler is raised when the system loses the network connection and the ononline handler is raised when the connection is restored. You can attach event listeners and be warned instantly about the state of the connection so that you can provide you users a blended experience during both online and offline functionality. Figure 11 shows a simple usage of ononline and onoffline events defined in the body of a Web page. Each time the user gets connected to the network, the Online JavaScript method will launch. msdnmagazine.com There are two rules you really should follow when defining timeout settings: the timeout event listener should be bound before opening the request and the timeout property should be set after opening the request. One of the major problems you will encounter when designing your AJAX applications involves communicating with services in various remote domains thanks to the same-origin-policy (see msdn.microsoft. com/magazine/cc163300 for more on the same-origin-policy). There are some workarounds you could perform, but none of them provide what you need: both a straightforward and a secure method of communication. Internet Explorer provides a new client-side object called XDomainRequest, which allows developers to access remote domains without requiring implementation of a a server-side proxy. If the remote server provides an Http Header called Access-Control-Allow-Origin:* all other remote sites will be able to request data from the current Web page. The client-side use of XDomainRequest object is the same as the ordinary XmlHttpRequest. Wrap-Up As you can see, Internet Explorer offers a number of features that solve many problems Web developers have been hoping to solve for a long time. Whether it’s in more deeply integrating your content and services into the browser or leveraging new browser platform services to provide for a more seamless user experience, Internet Explorer gives you the tools to build richer, more seamless experiences for your users. DARON YÖNDEM is the founder of Deveload Software based in Turkey. He is an MVP in ASP.NET and INETA Turkey Lead. Daron is an international speaker and writer of two ASP.NET AJAX books. March 2009 55 http://www.msdn.microsoft.com/magazine/cc163300 http://www.msdn.microsoft.com/magazine/cc163300 http://msdn.microsoft.com/library/cc304127 http://msdn.microsoft.com/library/cc304126 http://www.msdnmagazine.com
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.