AJAX: Have to wait for it to complete before proceeding?

1

1kHz

Hi all..

I'm new with this Ajax thingy and have done some experimenting. I did
some codes according to examples from
http://www.webpasties.com/xmlHttpRequest/
and
http://www.jamesdam.com/ajax_login/login.html

In my code, I request a PHP page that will iterate 10000 times and
return a 10000 line of text, and then put it in a DIV. I initiate the
Ajax from a button click. What I notice is, if I click some other
hyperlink in the page after clicking the button, it will wait until all
the Ajax processes are done before going to the link. Is this the norm?
Where's the "synchronous" part in that? Have I done something wrong?

These are some snippets from my code:
-------------------------------------------------
var req = new XMLHttpRequest();

function GetContent()
{
req.open("GET", "test2.php", true);
req.onreadystatechange = ReadyStateChange;
req.send(null);
}

function ReadyStateChange()
{
var panel = document.getElementById('theDiv');
if (req.readyState == 4)
{
panel.innerHTML = req.responseText;
}
}
 
1

1kHz

Where's the "synchronous" part in that? Have I done something wrong?
Sorry, that should be "asynchronous"..
 
R

Rich Hephner

Hi all..


I'm new with this Ajax thingy and have done some experimenting. I did
some codes according to examples from
http://www.webpasties.com/xmlH­ttpRequest/and
http://www.jamesdam.com/ajax_l­ogin/login.html


In my code, I request a PHP page that will iterate 10000 times and
return a 10000 line of text, and then put it in a DIV. I initiate the
Ajax from a button click. What I notice is, if I click some other
hyperlink in the page after clicking the button, it will wait until all

the Ajax processes are done before going to the link. Is this the norm?

Where's the "synchronous" part in that? Have I done something wrong?


These are some snippets from my code:
------------------------------­-------------------
var req = new XMLHttpRequest();


function GetContent()
{
req.open("GET", "test2.php", true);
req.onreadystatechange = ReadyStateChange;
req.send(null);
}


function ReadyStateChange()
{
var panel = document.getElementById('theDi­v');
if (req.readyState == 4)
{
panel.innerHTML = req.responseText;
}
}
------------------------------­-------------------


Thanks..


I'm new at this too, but that last argument in req.open is where you
specify synchronous or asynchronous. I'm betting if you change:

req.open("GET", "test2.php", true);

to

req.open("GET", "test2.php", false);

You'll get what you need.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top