A beginner AJAX tutorial

Discussion in 'JavaScript' started by hamidof, Sep 17, 2007.

  1. #1
    hamidof, Sep 17, 2007 IP
  2. Parasite

    Parasite Active Member

    Messages:
    192
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #2
    Thanks it was very helpful and now i know ajax =D
     
    Parasite, Sep 17, 2007 IP
  3. hamidof

    hamidof Peon

    Messages:
    619
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks! I will write more soon.
     
    hamidof, Sep 17, 2007 IP
    nwk likes this.
  4. nwk

    nwk Well-Known Member

    Messages:
    385
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    158
    #4
    Very nice tutorial..I Just learned AJAX basics in a few minutes..Thanks..
    M Waiting for continuation..lol
     
    nwk, Sep 17, 2007 IP
  5. soulscratch

    soulscratch Well-Known Member

    Messages:
    964
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    155
    #5
    You didn't learn AJAX (It's not a language). You learned how to use the built-in functionality of AJAX in the Yahoo library with deprecated HTML.
     
    soulscratch, Sep 18, 2007 IP
  6. hamidof

    hamidof Peon

    Messages:
    619
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Deprecated HTML?!!!
     
    hamidof, Sep 18, 2007 IP
  7. soulscratch

    soulscratch Well-Known Member

    Messages:
    964
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    155
    #7
    ^ Inline JS, No doctype, language attribute on script element
     
    soulscratch, Sep 22, 2007 IP
  8. hamidof

    hamidof Peon

    Messages:
    619
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #8
    This is to show how AJAX/XHR works in a very simple way, it's not an HTML nor JavaScript tutorial...
     
    hamidof, Sep 22, 2007 IP
  9. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Soulscratch has a point, you just showed them how to use YUI; not a real AJAX request.
     
    MMJ, Sep 23, 2007 IP
  10. hamidof

    hamidof Peon

    Messages:
    619
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #10
    It's still an AJAX request.

    YUI normalizes the whole thing so your code covers a wide range of browsers and as long as you are using this great code library, you can think of more abstract high level solutions and let YUI deal with those little details.

    That is why vBulletin, cPanel and Paypal (many more giants) are now using YUI.

    Also this is one of the best practices in programming, reuse great code.
    http://en.wikipedia.org/wiki/Code_reuse
     
    hamidof, Sep 23, 2007 IP
  11. hamidof

    hamidof Peon

    Messages:
    619
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #11
    It's AJAX which stands for Asynchronous JavaScript and XML, here is a little quote from Wikipedia:

    http://en.wikipedia.org/wiki/Ajax_(programming)

    No where in AJAX description states that you have to write all the XMLHTTPRequest stuff yourself then it's *real* AJAX :)
     
    hamidof, Sep 23, 2007 IP
  12. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #12
    well in that case all I have to do is write a function that makes an ajax request.

    so for you to "know" ajax all you have to do is write the name of the function.

    example:
    
    var xmlHttp; //Put it outside the function so it stays global
    function ajax() //Put the xmlhttp option in a function to reduce the code.
    {
    	try // Firefox, Opera 8.0+, Safari
    	{
    		xmlHttp=new XMLHttpRequest();
    	}
    	catch (e) // Internet Explorer
    	{
    		try
    		{
    			xmlHttp=new ActiveXObject("MSXML2.XMLHTTP");
    		}
    		catch (e)
    		{
    			try
    			{
    				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    			}
    			catch (e)
    			{
    				alert("Your browser does not support AJAX!");
    				return false;
    			}
    		}
    	}
    }
    function ajaxr(url)
    {
    	ajax();
    	xmlHttp.onreadystatechange=function()
    	{
    		if(xmlHttp.readyState==4)
    			alert(xmlHttp.responseText);
    	}
    	xmlHttp.open("GET",url,true);
    	xmlHttp.send(null);
    }
    PHP:
    so all you have to do to "know" ajax is write?:

    
    ajaxr('path/to/somefile.html');
    
    PHP:
    I disagree.
     
    MMJ, Sep 24, 2007 IP
  13. hamidof

    hamidof Peon

    Messages:
    619
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #13
    I'm well aware that AJAX is a technique, I'm writing more tutorials about how to use it, this one is just to get it started.

    Thanks for all the comments anyway...

    BTW your code is broken if you have to make multiple simultaneous requests.
     
    hamidof, Sep 24, 2007 IP
  14. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Whatcha mean?
     
    MMJ, Sep 24, 2007 IP
  15. strivinglife

    strivinglife Peon

    Messages:
    250
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #15
    I think his concern is being able to refer to this object elsewhere.

    As the code is, ajaxr('path/to/somefile.html'); results in an alert of the responseText.

    Of course, that doesn't help too much unless we want to output this to the user. What we really want to be able to do is store that and then parse through it to display/evaluate the necesary information.

    His question is, I think, how can we use this multiple times, once right after the other, and have two different XML files available for display?
     
    strivinglife, Oct 16, 2007 IP