A Look At Ajax: Split Identity
While looking into Ajax in my spare time (which, admittedly, I haven't had too much of lately), I found out that this is another case where Internet Explorer and the rest of the browser world. The basis of the functionality of AJAX is the XMLHttpRequest object, which is built in JavaScript. However, IE builds this object differently from most other browsers by using ActiveXObjects to do so:
<script>var xmlHttp = false;try {xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");} catch (error) {try {xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");} catch (error2) {xmlHttp = false;}}</script>
Now, you may be wondering why there are two different types of calls to create our XMLHttpRequest object. The reason is that Microsoft's browsers support both XML, an international standard, as well as MSXML, which is an extension on XML designed to help it play nicer with non-browser applications like Microsoft Office.
Now, if we reach the point in the try statement where xmlHttp is assigned to false, then we're likely dealing with a non-IE browser. If we're working with another browser, then we're only going to create one kind of XMLHttpRequest object, which we can do in a single line:
<script>var xmlHttp = new XMLHttpRequest object;.</script>
With these two different standards for the groundwork of AJAX, we're going to need a way to support any AJAX-enabled browser. Otherwise, a number of people may never have access to certain features of the site.
<script>var xmlHttp = false;try {xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");} catch (error) {try {xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");} catch (error2) {xmlHttp = false;}}
if (!xmlHttp && typeof(XMLHttpRequest) != 'undefined') {xmlHttp = new XMLHttpRequest();}</script>
The reason we don't immediately assume that we can use the non-Microsoft browser implementation of the XMLHttpRequest object is because there is also the chance that the browser being used does not support the use of AJAX. In that case, we do not want to try and build the XMLHttpRequest object because the page will throw an error.
Now that we have the basic object for AJAX functionality, we will next need to put it to use. In the next part of this series, we will look to do just that.


There are no comments for this entry.
[Add Comment]