Go Daddy $1.99 Domains
 
Home Register FAQ Members List Calendar Search Today's Posts Mark Forums Read Web Directory

Go Back   Webmaster Forum > Designing and Developing Websites > Programming > Ajax / Javascript

Ajax / Javascript Discussions and help on Ajax and Javascript coding.

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 03-10-2007, 03:44 AM
spinker.spanker spinker.spanker is offline
Persona Non Grata
 
Join Date: Mar 2007
Posts: 72
iTrader: (0)
spinker.spanker is on a distinguished road
Default A question on AJAX

I was looking through DynamicDrive's AJAX section and found a script that caught my eye. i had a look at the code for it and found something odd. it had no onreadystatechange function. instead it had this:

Code:
page_request.open('GET', url, false) 
page_request.send(null) 
someFunction(page_request)
So my questions are as follows.
Is onreadystatechange actually needed?
What are the advantages of it? and
Why do most AJAX scripts use it at all if it is not necessary?
Reply With Quote
Sponsored Links
Register and sign in to hide this ad block

  #2 (permalink)  
Old 03-10-2007, 05:57 AM
killerkev06 killerkev06 is offline
Persona Non Grata
 
Join Date: Mar 2007
Posts: 124
iTrader: (0)
killerkev06 is on a distinguished road
Default

onReadyStateChange

As the name kind of implies, every time the "ready state" changes this function will be executed.

When the property readyState is 4 that means the response is complete and we can get our data.

Then the function that is repeatedly called can use the information depending on the readyState.

This basically ensures that only if the request has completed correctly your script will execute as you wish.

Kev
Reply With Quote
  #3 (permalink)  
Old 03-10-2007, 07:03 AM
spinker.spanker spinker.spanker is offline
Persona Non Grata
 
Join Date: Mar 2007
Posts: 72
iTrader: (0)
spinker.spanker is on a distinguished road
Default

Thanks for the response but it wasn't really what i was looking for. let me rephrase the question:
why use onreadystatechange if there is an alternative which doesn't require checking the page request to see if it has finished?
Reply With Quote
  #4 (permalink)  
Old 03-10-2007, 11:22 AM
killerkev06 killerkev06 is offline
Persona Non Grata
 
Join Date: Mar 2007
Posts: 124
iTrader: (0)
killerkev06 is on a distinguished road
Default

Most AJAX scripts will run asynchronously - so you can send off the request to the server in the background and carry on doing things at the client end, waiting for a response from the server - this is when you need the onreadystatechange.

However, 'AJAX' scripts can also run 'synchronously' (which is kinda AJAX without the 'A'?!) - in which case the script waits for a response from the server, no background processing, your script is stuck until you get a response. No need for the onreadystatechange.

Quote:
page_request.open('GET', url, false)
With a 3rd parameter set to false, the script seems to be opening a synchronous request - so no need for onreadystatechange in this case (AJAX without the 'A'). This needs to be true for a regular asynchronous AJAX request.

(Well, I believe that's the idea... I'm no AJAX expert!)
Reply With Quote
  #5 (permalink)  
Old 03-13-2007, 07:51 AM
Tripy Tripy is offline
WMG Newcomer
 
Join Date: Mar 2007
Posts: 31
iTrader: (0)
Tripy is on a distinguished road
Default

You have got it perfectly right KillerKev.
With the 3rd paramter set to false, the request is made synchronously, and that's why the onreadystatechange is not needed.

When doen asynchronously, the request is fired inbackground and more or less "forgot".
The onreadystatechange is there to get feedback and catch teh datas back.

In a synchronous mode, there is no need to watch the state, therefor the lack of return handler function.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

All times are GMT. The time now is 04:12 PM.



Freelance Web Designers
Work At Home Forum
Ad Marketplace
Online Deals and Bargains
HostGator Reseller Hosting Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.0.0