tags:

views:

61

answers:

1

Hi!

It's a weird doubt, but it goes....

I have a button in a page, that makes a Ajax request, and when i have the answer, I write it to the page and then it's called a function to adjust Page Elements. When i click the button the first time, this function isn't called, but in the next request it is.

But...if I place a alert in the scope of the function, when i make the same first ajax request and wait for the answer to call the function, it displays the content of the alert and adjust my page elements.

Well...i don't see any logic in that, but if this ring's a bell to anyone, I apreciate the help.

EDIT:

function HandleResponseCellVehicleReport() 
{
    if (XmlHttp.readyState == 4) {
        if (XmlHttp.status == 200) {
        WriteCellVehicleReport(XmlHttp.responseText);
        }
        else {
            alert("Problema: " + XmlHttp.statusText);
        }
    }
}

function WriteReport(myJSONtext) {
    (....)
    EndLoad(document);
    EnableButtons(document);
    adjustPageElements(pageId,document);
}

function adjustPageElements(client, __document) {

    var viewportwidth;
    var viewportheight;

    if (!__document) {__document = document;}
    (....)
}

I place the alert in the beginning of adjustPageElements function.

Thanks

+3  A: 

I think this is because you ajax call goes Asynchronous, the content isn't yet fetched from the backend script before you try to put it in a page element. Take a look at this. If you make a synchronous ajax call the browser waits until the content is parsed back. also see this article

Tim
Thanks Tim, I'm going to read that and later I'll give some feedback.
Ruben F.
That's true but synchronous ajax calls are generally a bad idea.
Pointy
Indeed, be careful with synchronous calls. If the ajax call fails the browser freezes until an user refreshes the page, something you don't want. If in rare cases an synchronous call is necessary most of the time the users input is disabled until the call is completed.
Tim