JavaScript
  Home arrow JavaScript arrow Page 3 - JavaScript Remote Scripting: Reading Data ...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

JavaScript Remote Scripting: Reading Data From the Server
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 26
    2005-09-21

    Table of Contents:
  • JavaScript Remote Scripting: Reading Data From the Server
  • A non-standard approximation: reading server data with AJAX
  • Checking asynchronous http requests: taking a look at the "statusChecker()" function
  • Displaying file contents: defining the "createDataContainer()" and "displayData()" functions
  • Reusing the source code: listing the full script

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    JavaScript Remote Scripting: Reading Data From the Server - Checking asynchronous http requests: taking a look at the "statusChecker()" function


    (Page 3 of 5 )

    One of the best things that AJAX offers is the ease in controlling the status of a specific request through the "onreadystatechange" event handler, which, when used in conjunction with the "readyState" and "status" object properties, allows you to track easily the progress of a request.

    Based on this functionality, it's possible to define a function that checks for the progress of an active petition, in order to move program control to specific functions, once the request has been completed.

    That's where the "statusChecker()" function comes in. Since the script's logic is built around this function, after the request is successfully performed and file contents have been read, it will invoke a couple of additional functions that will be reviewed briefly. Now, let's take a look at the "statusChecker()" function, which is listed below:

    function stateChecker(){
        // if request is completed
        if(xmlobj.readyState==4){
            // if status == 200 display text file
            if(xmlobj.status==200){
                // create data container
                createDataContainer();
                // display data into data container
                data=xmlobj.responseText.split('|');
                displayData();
            }
            else{
                alert('Failed to get response :'+ xmlobj.statusText);
            }
        }
    }

    As you can see, the function above checks to see whether the request has been successfully completed, by verifying the values of "readyState" and "status" properties. After checking for a successful request, the function calls in turn to "createDataContainer()" and "displayData()" functions, which, as you'll see in a moment, create a simple <div> containing element and implement the news rotator, based on the contents read from the data file.

    Of course, you'll be wondering how the contents are processed to be displayed in sequence. Notice the following line:

    data=xmlobj.responseText.split('|');

    In my first attempt to display file contents, I create a data file where headlines are separated from each other by a pipeline character (|). Given that, the above line stores each headline as a separated array element, by splitting the value of the "responseText" property.

    Considering the file format expected by the script, a sample news file would look like this:

    headline 1|headline 2|headline 3|...headline N

    It's worthy noting that a news file might be easily defined as an XML file rather than troubling it with pipeline delimiters. Don't worry about this for the moment; remember that I'm providing this example as an introductory process to using more robust data containers, including XML files and database tables.

    At this point, the script is able to request a given data file, and then process the data back in the client. Thus, the next thing to be done is to define the remaining functions for building a <div> container and displaying the headline rotator.

    More JavaScript Articles
    More By Alejandro Gervasio


       · This first tutorial introduces the basics for fetching files from the server through...
       · Your code assumes that AJAX is not possible in a non-ActiveX IE.According to...
       · Hello Kae,I wrote the script by assumming the availability of ActiveX in IE6....
       · I thank you for your really cool article about AJAX. For me it's kinda new and...
       · Hi there. His follow up article should come 2 weeks after this one. Usually, a...
       · Hello,Many thanks for the kind words about the introductory AJAX series. Of...
     

    JAVASCRIPT ARTICLES

    - Validating Digits and Dates with jQuery`s Va...
    - Validating Ranges, Emails, and URLs with jQu...
    - More Uses for the jQuery Tooltip Plug-in`s b...
    - Building Image-Based Tooltips with the jQuer...
    - Using the jQuery Tooltip Plug-in`s bodyHandl...
    - Using Rangelength, Min and Max with the Vali...
    - Using Minlength and Maxlength with the Valid...
    - Modifying Tooltip Coordinates with the jQuer...
    - Applying a Fade Out Effect with the jQuery T...
    - Tracking Mouse Movements with the jQuery Too...
    - Checking Online Forms with the Validator jQu...
    - Nested JavaScript Functions as Objects
    - The jQuery Tooltip Plug-in
    - Active Client Pages at the Server
    - ACP Tab Web Page







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT