SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Nov 2011
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ajax function question

    I'm trying to figure out why my ajax script is not working. For example only (not complete code):

    request = new XMLHttpRequest();
    request.onreadystatechange = function () {
    if(request.readyState == 4) {
    if(request.status ==200) {
    some code }


    Can I name this function by putting a name between the word "function" and the "()" on the

    "request.onreadystatechange = function() {" line above, then call it by name with an event handler?

    Even though this code is incomplete, did I make any mistakes?

    Thanks

  2. #2
    SitePoint Addict sdleihssirhc's Avatar
    Join Date
    Feb 2009
    Posts
    387
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Can I name this function by putting a name between the word "function" and the "()"... then call it by name with an event handler?
    Nope. Try the following for proof:

    Code JavaScript:
    request = new XMLHttpRequest();
    request.onreadystatechange = function test() {
        alert('WAZZA');
    };
     
    alert(test); // ERROR: "test is not defined""

    The good news is there's a pretty easy way to do what you want: Define the function first, and then assign it:

    Code JavaScript:
    function test() {
        alert('WAZZA');
    }
     
    request = new XMLHttpRequest();
    request.onreadystatechange = test;
     
    someElement.addEventListener('someEvent', test, false);

    Even though this code is incomplete, did I make any mistakes?
    You didn't use the "var" keyword to create your request variable. When you assign something to a variable without using "var", you actually create a global variable. So it's not really a mistake per se, as long as you're aware of what you're doing, but it makes the hairs on the back of my gritted teeth stand on end.

    It looks like you never closed the onreadystatechange function, but that could just be because your sample is incomplete.

    As it stands, there's no support for IE6, which doesn't have the XMLHttpRequest function. You need to use ActiveXObject instead:

    Code JavaScript:
    request = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');

    ...But that's only if you care about IE6. If you don't, then no worries!
    I'm the web overlord for Graphic Business Systems

  3. #3
    SitePoint Member
    Join Date
    Nov 2011
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Very helpful. Thanks for taking the time.

  4. #4
    SitePoint Member
    Join Date
    May 2013
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For supporting ajax2 in IE (before 10) you need to use XDomainRequest instead.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •