AJAX XMLHttp Request Aid

By vaseline28 on Sep 13, 2008

I find that lots of people new to AJAX can't get the grips because they don't understand the XMLHttpRequest, below is a help to that - this will handle the request, all that you have to fill in is the Commands to be executed when the content is got.
You also have to add in where to get the content from, but it is explained in comments :-)

function ajaxXML_requestCreate()
{
  // Detect Browser for XMLHttpRequest (IE uses a different method to other browsers)
  var xmlHttpForm;
  if (navigator.appName == "Microsoft Internet Explorer")
    xmlHttpForm = new ActiveXObject("Msxml2.XMLHTTP");
  else
    xmlHttpForm = new XMLHttpRequest(); 
  return xmlHttpForm;
}
// This section calls the AJAX Request when the page loads so that we don't have to use it again.
var xmlHttp = ajaxXML_requestCreate();

function ajaxXML_Request()
{
  /* Where to get the content from, with this - you are given several choices:
   xmlHttp.open("GET",URL_ofPage,true);
   You can specify "GET" or "POST" for $_GET or $_POST methods. URL_ofPage is where the URL of    the page to get the data from should be put. */
  http.onreadystatechange = ajaxResponse(locat);
  // Replace 'locat' with the id the response must go to.
  xmlHttp.send(null);
}

function ajaxResponse(locat)
{
   // Variable: locat :is the id of the location you want the response to go to. 
    if(xmlHttp.readyState!=4)
       {
       /* Commands in this section of the script will be triggered when the content is being                     fetched, but has not come through yet.
      For example, you might want to put in:
      document.getElementById('locat').innerHTML="**Content loading...please wait**"; */
       }
    else
       {
          var response = http.responseText;
          // Commands to be done with the content retrieved
          // The variable in which the response is stored is 'response'
       }
}

Comments

Sign in to comment.
Hawkee   -  Sep 30, 2008

Looking better vaseline, but there is still some room for improvement. Your ajaxXML_Request() function should take URL_ofPage as an input so a dynamic document URL can be called. It would also be helpful to see the HTML code to call this. And finally, your spacing is a bit off with the brackets not lining up properly.

 Respond  
vaseline28   -  Sep 30, 2008

Updated a bit to be more similar to what Hawkee recommended.

 Respond  
vaseline28   -  Sep 21, 2008

Mmmm, I've got round the problem by doing:

echo "<body onload=" . $qt . "ajaxGenerate_Content('ajaxContent_generate','/Site/ajaxCon/" . $_GET['value'] . ".php?mthd=AJAX')" . $qt . ">";

Obviously with a case for if $_GET['value'] is not a recognised file, that way someone can link to a specific page with:
http://www.vasey.co.cc/Site/?value=Scripting

 Respond  
Hawkee   -  Sep 14, 2008

I'm just saying that if you'd like people to bookmark a page, or if you'd like the search engines to spider the page, you just can't link to it with AJAX.

 Respond  
Jonesy44   -  Sep 14, 2008

yeah, but people are too picky..
If you were gonna link someone to a site like omega, you'd know the page linking system. else, there's no need. simple logic.

Anyways, there's not much to it..

 Respond  
Hawkee   -  Sep 14, 2008

Yeah you need to be careful with AJAX because you need to maintain a proper URL to page ratio. If you make each of your pages an AJAX call then you won't have separate URLs. It's better to use AJAX only when it doesn't adversely affect the URLs.

 Respond  
vaseline28   -  Sep 14, 2008

That's very true, another way to do it would be to add in a Javascript script to pages like this:
http://omega.neeq.net/home.php
So if the URL is that it will redirect them to:
http://omega.neeq.net/?page=/home.php

It's also a pain to make it work for Search Engine crawls...

 Respond  
Jonesy44   -  Sep 14, 2008

lol, thanks!

Such as; you can't link people to it. it's still http://omega.neeq.net even if you're on downloads page. now to me, that's no biggy cos if you're linking out as the admin, i've made a function;

?page=/pagename

 Respond  
vaseline28   -  Sep 14, 2008

Yeah, I'd noticed that. Of course there are drawbacks, which particular ones did you encounter?

And site looks great btw. I remember when Lindrian pointed me to it...'Wow! That's awesome'

 Respond  
Jonesy44   -  Sep 14, 2008

Neat! Nice work bud. I used a lot of ajax on lindrian's site. although i now realise it does have some drawbacks;

http://omega.neeq.net

 Respond  
vaseline28   -  Sep 14, 2008

Thanks jonesy :)
I'm starting to get the hang of it, so if you do need help, feel free to let me know...'course there are no guarantees that I can ACTUALLY help you!

Edit: I've added in:

  if(xmlHttp.readyState!=4)
    {
      // Commands in this section of the script will be triggered when the content is being fetched, but has not come through yet.
     // For example, you might want to put in:
     // document.getElementById('example').innerHTML="**Content loading...please wait**";
    }

This script is taken from my webpage which is currently in development - you can see the extent of even very basic AJAX here: http://www.vasey.co.cc/Site/

 Respond  
Jonesy44   -  Sep 14, 2008

-

 Respond  
vaseline28   -  Sep 14, 2008

I see, OK, I'll modify this when I get back - have to go out now.

 Respond  
Hawkee   -  Sep 13, 2008

You don't really need to get xmlHttp every time an AJAX call is made. That can be done outside the function upon page load. I also think you should have done onreadystatechange to a function that handles the response, like this:

...
element = 'mydiv';
xmlHttp.onreadystatechange = handleResponse;
xmlHttp.send(null);

function handleResponse()
{
    if(xmlHttp.readyState == 4)
    {
        var response = xmlHttp.responseText;
        document.getElementById(element).innerHTML = response;
    }
}

This example will only work if xmlHttp and 'element' are declared globally.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.