Section 2.31. Frequently Asked Questions?


2.31. Frequently Asked Questions?

Q:

How does the server run the callback function when the ready state changes? I didn't think the server could call JavaScript code that's in an HTML page.

A:

You're right...it's the browser that actually runs the callback function. When the server is done with a request, it lets the browser know. At that point, the server's finished, and it's the browser's job to figure out what to do next. The browser looks up the function specified in the onreadystatechange property of the request object, and calls that function. So it's the browser running your JavaScript code, not the server.

Q:

Will we ever need to write code that does something when the ready state isn't 4?

A:

Not very often. Remember, these are asynchronous requests, so your users aren't waiting around on the server, and don't really need to know what state the request is in. You'll usually code your callback to only take action when the server is finished with the request, and the ready state is 4.




Head Rush Ajax
Head Rush Ajax (Head First)
ISBN: 0596102259
EAN: 2147483647
Year: 2004
Pages: 241

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net