Web site designers - 268 CHAPTER 9 THE AJAX APPROACH TO
268 CHAPTER 9 THE AJAX APPROACH TO BROWSER SCRIPTING In Mozilla, Opera, and Safari, you need to use xmlhttp = new XMLHttpRequest(); You can create a cross-browser version using the following code: if (window.XMLHttpRequest) { // we have Mozilla, Opera or Safari xmlhttp = new XMLHttpRequest(); } else if (window.ActiveXObject) { // we have IE try { xmlhttp = new ActiveXObject(”Msxml2.XMLHTTP”); } catch(e) { try { xmlhttp = new ActiveXObject(”Microsoft.XMLHTTP”); } catch(e) { xmlhttp = false; } } } Tip In the Mozilla code, you may need to include a call to the overrideMimeType() method if you want to ensure that non-XML data is returned correctly: xmlhttp.overrideMimeType(’text/xml’); Once you create the object, you should set the onreadystatechange event handler before making the request. I ll cover that shortly. When you have the object and event handler, you can make the request and optionally send data to the server: xmlhttp.open(”GET”, “dvd.xml”, true); xmlhttp.send(null); You use the open()method to make the request. This might be a GET, POST, or HEAD request, and you set the request type in the first parameter as a string value. The second parameter is the page you re requesting. In the preceding code, I ve referred to a static page, but you could also request content from a server-side page. The last parameter sets the request to be asynchronous. You should set this value to true or asynchronous. If you create a synchronous call, you run the risk of a server problem stopping the execution of the remainder of the page.
In case you need quality webspace to host and run your web applications, try our personal web hosting services.