Beginners AJAX Tutorial Part 2

Ajax Avenue Street Sign


This is part 2 of the AJAX Tutorial Series. You can see the Beginners AJAX Tutorial Part 1 here.

In this part, we will start you off with some very simple code just to show you how things get started. At the end of this tutorial you will have made your own ‘hello world’ AJAX script… (not a big deal I know, but you have to start somewhere!). I’ll keep these tutorials bitesize and introduce a new command or concept on each tutorial to add to what has already been covered. Today, we start at the beginning with something that will be in all future scripts…

The XMLHttpRequest Object

The what? :-)
The XMLHttpRequest object is at the heart of AJAX. It allows you to send and receive data with javascript without refreshing the page and works on all browsers (except, you guessed it! Internet Explorer). All you have to do is find out if the browser supports it with an ‘if’ statement in the javascript contained in the <head> part of the html page and then create a XMLHttpRequest object using the ‘new’ command;

if (window.XMLHttpRequest) {
        XMLHttpRequestObject = new XMLHttpRequest();}

That would create a XMLHttpRequest object and store it in XMLHttpRequestObject.

But, what about Internet Explorer?.. We can’t use XMLHttpRequest so we have to use an ActiveXObject. For us to do anything with an XMLHttpRequest and make sure it works on both IE and Firefox, we can add this to the javascript so we now have..

if (window.XMLHttpRequest) {
        XMLHttpRequestObject = new XMLHttpRequest();
      } else if (window.ActiveXObject) {
        XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
      }

OK, that should cover us for making a XMLHttpRequest object, we can’t do anything in this tutorial series without it so to be sure, lets just create the whole html document and test to see if it can make an object by adding another ‘if’ statement to output some text to the screen if the object creation was successful using the javascript command ‘document.write()’.


    <title>FiddyP Ajax Tutorial Part 2 - Test object creation</title>

      var XMLHttpRequestObject = false;

      if (window.XMLHttpRequest) {
        XMLHttpRequestObject = new XMLHttpRequest();
      } else if (window.ActiveXObject) {
        XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
      }

      if (XMLHttpRequestObject) {
        document.write("<h1>Hello World! I Created an Object!</h1>");
      }

You can click on the title of the code snippet above and see the plain text version, just copy this to your own file and save it as an HTML document or right click here and save it and test it. If it’s working, it will display the message “hello world! I created an object!” in big letters.

YEY! we created a ‘hello world’ AJAX program!, I know it’s not much use yet but this is the foundation for all future tutorials so even thought it’s not that ‘sexy’ , it IS important to make sure we can create the object that will handle the data being sent to and received from a remote file.

We can get into the sexy ins and outs of using this on the next tutorial. Please come back again to see how we can use this to open a remote file and display it’s contents on a HTML page without a reload…(ooh, I can’t wait….)
:-)

About Andy

This is my personal website where I (occasionally) post about things going on in the world of me. I am the creator of CommentLuv and administrator of comluv.com
ajax, Code , , , , , , ,

0 responses to Beginners AJAX Tutorial Part 2


  1. The second part came in 13 days… I had to read back the first part to get in sync.. anyhow revision always helps…

    I don’t remember the Enable CommentLuv checkbox below the submit button. Is it so that earlier the page was fetched on server end and latest version uses client to do so.

    jalaj’s last blog post..Int() and Val() in JavaScript

  2. Sorry Jalaj, I was so busy with work that I didn’t have time to put the next part up until yesterday!
    i am giving CommentLuv a test for a few days on here before I release the new version. The checkbox was requested by someone who didn’t always want their comment to have a last blog post. Plus, it’s better to give people the choice. Also, I have added another routine that should make it more compatible with hosting that doesn’t allow CURL commands. I’ll post about it this week…
    thanks for visiting!

  3. Collect it in my binder done. Now, I can say hello to Ajax.
    Still waiting for next lesson, guru. :D

  4. Agen Iklan » I have just written part 3 of the series and saved it ready for publishing next week so there wont be such a long wait this time!
    thanks for visiting Agen, I hope you get some use out of these tutorials..

  5. Hi, Andy, did you see yourself on my blogroll?
    You’ve been an amazing resource for my php neediness.
    I’m a leeetle nervous about implementing the final code, but it isn’t anything that a Ctrl-z wouldn’t cure anyway.
    Thanks again.

    witchypoo’s last blog post..Working with the police, part one

  6. I saw my site thumbnail there thanks!
    you should be able to run the code on your own PC, no need to upload it anywhere!

  7. Yes a part two! I’m printing this and will be perusing it while I’m in the sauna this morning.

    Opal Tribble’s last blog post..Guest Posting & Exercise From a Vegan Perspective

  8. ahhh, sauna and source code, a hot bit of AJAX.. Yum :-)
    The next 2 parts will definitely be on time, I have them both saved as drafts ready to publish, they’re still not ‘look-at-what-a-cool-ajax-script-I-wrote’ yet but I think it’s important to get the beginning stuff done a little bit at a time.. by part 6 you’ll be able to put a useful script on a page and call it your own..

  9. Pingback: 13 Great Articles - December 07, 2007 | My lucky number 13

  10. Anish R Nair

    I have to use two ajax functions in an asp page not in asp.net, how i hve to create and use it

  11. Jeff

    It allows you to send and receive data with javascript without
    refreshing the page and works on all browsers
    (except, you guessed it! Internet Explorer)

    Huh? The XMLHttpRequest was written by Microsoft… why wouldn’t it work in IE?

  12. just using window.XMLHttpRequest wont work on internet explorer for this type of function, that’s why there is ActiveXObject(“Microsoft.XMLHTTP”) in there.
    it was however, originally developed by Microsoft as part of Outlook Web Access 2000, as a server side API call but Mozilla incorporated the first compatible native implementation of XMLHttpRequest in Mozilla 1.0 , IE needs to use activex to achieve the same functionality, IE also has issues with caching dynamic pages so even though it was originally written by microsoft, they don’t use it well in IE (coz it’s crap)

    happy now?

  13. Chaitanya Kulkarni

    I read 4th part which allow to Display records serially but how to delete particular record from all those using Ajax.