AJAX
  1. AJAX intro
  2. AJAX basics
  3. AJAX browsers
  4. AJAX forms
  5. AJAX sending data
  6. AJAX summary

AJAX browsers

When creating an AJAX object, you should always keep in mind that an AJAX object cannot be created the same way for all browsers.

This tutorial focuses on:

Creating an AJAX object for various browsers

You can have one set of code in Javascript that will dictate what kind of AJAX should object should be created based on what browser is being used. An AJAX object is created one way for Internet Explorer 5 & 6 and a different way for other browsers. Internet Explorer 5 & 6 use an ActiveXObject, while other browsers use XMLHttpRequest.

To deal with this duality, we will use a try.....catch block in Javascript. To learn more about try.....catch blocks, read our Javascript errors page.

Example:
<script type="text/javascript"> function ajaxDataGet(){ var ajaxObject; //the variable that will store the object try{ // Firefox, Safari, Chrome, Opera, Internet Explorer 7+ ajaxObject = new XMLHttpRequest(); } catch (e){ // Internet Explorer 5 & 6 try{ ajaxObject = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e){ //if AJAX is not supported at all alert("Your browser does not support AJAX!! How is it back in 1998?"); return false; } } } </script>

In the above example, the function ajaxData() is used to set up an AJAX object. The variable ajaxObject is used to store the AJAX object. The rest of the code invloves a try.....catch block that will create the AJAX object depending on whether the user is using Internet Explorer 5/6 or another web browser.

The try.....catch block says to first try to create an AJAX object using XMLHttpRequest. If that doesn't work (meaning the user is using either Internet Explorer 5/6 or a browser that doesn't support AJAX), create an AJAX object using ActiveXObject. If that doesn't work (meaning the user is using a browser that doesn't support AJAX), we issue the message "Your browser does not support AJAX!! How is it back in 1998?"

The above is the core code to create an AJAX object.

The list of steps to create an AJAX object:

You do not have to use the same exact code to create AJAX objects. But whatever code you use, the steps should be the same.

© Copyright 2013-2014 Landofcode.com
Terms of use | Privacy policy | Copyright information