SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    May 2008
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript/AJAX problem - only works after a page refresh

    I have a php page using some JavaScript and AJAX to take a string, search a database with it and then dynamically generate a SELECT menu in a web form based on the results. It works fine......but only after the page has been refreshed first.

    When the page is first called and I click on the button to trigger the script I get the error message, "myRequest is null or not an object". I've tested the variable type of 'myRequest' on the first call of the page and it is 'undefined'. If I refresh the page then the error disappears and the JavaScript and AJAX work perfectly, and the type of myRequest becomes 'object'.

    Given the the functionality of the script does work once the page has been reloaded, it's clear that the snippet of code which sets the 'myRequest' object only does so upon the page being refreshed. Here's the code that sets the object:

    var myRequest = getXMLHTTPRequestBrowser();

    And here's the function it's calling:

    function getXMLHTTPRequestBrowser() {
    var request = false;
    if (window.XMLHttpRequest) // is XMLHttpRequest a native object of browser (i.e. Mozilla, Safari etc.)?
    {
    request = new XMLHttpRequest(); //if so, use it
    } else { //if not,
    if (window.ActiveXObject)
    { // if IE
    try
    {
    request = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (err1)
    {
    try
    {
    request = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (err2)
    {
    request = false;
    }
    }
    }
    }
    return request;
    }

    I think I have narrowed down the problem to the line:

    if (window.XMLHttpRequest) // is XMLHttpRequest a native object of browser (i.e. Mozilla, Safari etc.)?
    {
    request = new XMLHttpRequest(); //if so, use it
    }

    This condition is being met by the page and is causing the 'undefined' error upon first call.

    Apart from bodging this by forcing the php page to recall itself upon loading, is there anything obvious here?

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can we see more of your code?


  3. #3
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    myRequest is null or not an object
    Means that you are trying to access a property or method of the variable myRequest that it doesn't have. But the code you display doesn't do that, so it must be that the variable is not properly initialized when you try to use it, elsewhere in your code.

    myRequest.open(blah...) // if myRequest was not initialized, I would expect your error to be displayed here

    It sounds like you're doing something in the wrong order. Try initializing/using myRequest at the bottom of your document or in the window onload event handler.


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
  •