JavaScript
  Home arrow JavaScript arrow Page 5 - 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  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
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 / 22
    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 - Reusing the source code: listing the full script


    (Page 5 of 5 )

    As I said before, below is the full source code for the JavaScript program that you just saw. As usual, feel free to tweak the script and modify it to fit your particular needs:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <title>REMOTE SCRIPTING WITH AJAX</title>
    <script type="text/javascript">
    // initialize XMLHttpRequest object
    var xmlobj=null;
    // initialize global variables
    var data=new Array();
    var i=0;

    // send http request
    function sendRequest(doc){
        // check for existing requests
        if(xmlobj!=null&&xmlobj.readyState!=0&&xmlobj.readyState!=4){
            xmlobj.abort();
        }
        try{
            // instantiate object for Firefox, Nestcape, etc.
            xmlobj=new XMLHttpRequest();
        }
        catch(e){
            try{
                // instantiate object for Internet Explorer
                xmlobj=new ActiveXObject('Microsoft.XMLHTTP');
            }
            catch(e){
                // Ajax is not supported by the browser
                xmlobj=null;
                return false;
            }
        }
        // assign state handler
        xmlobj.onreadystatechange=stateChecker;
        // open socket connection
        xmlobj.open('GET',doc,true);
        // send request
        xmlobj.send(null);
    }

    // check request status
    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 container
                data=xmlobj.responseText.split('|');
                displayData();
            }
            else{
                alert('Failed to get response :'+ xmlobj.statusText);
            }
        }
    }

    // create data container
    function createDataContainer(){
        var div=document.createElement('div');
        div.setAttribute('id','container');
        if(div.style){
            div.style.width='500px';
            div.style.height='45px';
            div.style.padding='5px';
            div.style.border='1px solid #00f';
            div.style.font='bold 11px Tahoma,Arial';
            div.style.backgroundColor='#eee';
            document.getElementsByTagName('body')[0].appendChild
    (div);
        }
    }

    // display data at a given time interval
    function displayData(){
        if(i==data.length){i=0};
        document.getElementById('container').innerHTML=data[i];
        i++;
        setTimeout('displayData()',20*1000);
    }

    // execute program when page is loaded
    window.onload=function(){
        // check if browser is DOM compatible
        if(document.getElementById &&
           document.getElementsByTagName &&
           document.createElement){
            // load data file
            sendRequest('technews.txt');
        }
    }
    </script>
    </head>
    <body>
    </body>
    </html>

    To wrap up

    That's all for now. In the second part of this article, I'll explain how to use JavaScript remote scripting for working directly with XML files by using AJAX technology. Also, I'll run through an illustrative example, helpful to implement within many client-side programs. Considering that remote scripting can boost considerably modern Web applications, the topic is worthy of review. See you soon!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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

    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget
    - Ajax Hack for Entering Information Without R...
    - EXT JS 2.1 Overview
    - Using the Style Object for Zebra Tables with...
    - Binary Searching
    - An Improved Approach to Building Zebra Tables






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT