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
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.
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:
- Create a function to set up an AJAX object
- Create a variable within that function to store the AJAX object
- Use a try.....catch block to try to crate an AJAX object using XMLHttpRequest
- If that doesn't work, create an AJAX object using ActiveXObject
- If that doesn't work, issue a message informing the user that their browser doesn't support AJAX
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.