1

Can someone please show me the basic syntax for loading a page via AJAX just using javascript-- not jquery? load() method?

xmlhttp=new XMLHttpRequest();

Where do I go from here?

  • a simple google search would give you a bunch of examples. Either way, it is quite a hassle, which is why people just use jquery or other libraries since it simplifies the whole process. here is a similar question on SO http://stackoverflow.com/questions/3038901/how-to-get-the-response-of-xmlhttprequest – kennypu Dec 28 '12 at 01:00
  • A good starting point would be to see how jQuery does it. Not to use it but to go to the actual source and read/observe how it's done there. It's a very good implementation, and the code is really clean. – Joel Etherton Dec 28 '12 at 01:00
  • @kennypu That's the thing! Nothing but jquery load() examples in my search results. The dirty, old fashioned wayhas been buried, it seems... – user1933397 Dec 28 '12 at 01:03
  • @JoelEtherton My failings with jquery are the reason why I'm asking! Is the lesson at http://www.w3schools.com/xml/xml_parser.asp still relevant? – user1933397 Dec 28 '12 at 01:06
  • This is some good reference: https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest/Using_XMLHttpRequest – bfavaretto Dec 28 '12 at 01:09
  • @user1933397: If it comes from w3schools I'd stay away from it. Some of it's right, but some of it's misleading. It's better to use the mozilla documents suggested by bfavaretto. – Joel Etherton Dec 28 '12 at 01:13
  • @user1933397 if i literally search "XMLHttpRequest() tutorials" I get a bunch of relevant examples.. – kennypu Dec 28 '12 at 01:14
  • @kennypu Thanks for pointing out a good google search term,lol. "A well defined problem is half solved"...so they say...*palm to forehead* – user1933397 Dec 28 '12 at 01:19

1 Answers1

0

You will write more lines without jQuery, in jQuery it will done in 2 lines.

Anyway, here is the code

without jQuery

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function()
{
   if (xmlhttp.readyState == 4)
   {
        alert(xmlhttp.responseText);
    }
}
xmlhttp.open('GET', 'http://www.google.com', true);
xmlhttp.send(null);

if you want to do some POST stuffs

xmlhttp.open("POST",'http://www.google.com', true);
xmlhttp.send();

You also need to do checking for IE

see this link for more understanding - XMLHttpRequest

with jQuety

$.get('http://www.google.com', function(responseText) {
    alert(responseText);
});
AMIC MING
  • 6,306
  • 6
  • 46
  • 62