Home arrow JavaScript arrow Page 5 - JavaScript Remote Scripting: Reading Data From the Server
JAVASCRIPT

JavaScript Remote Scripting: Reading Data From the Server


Client-server interaction achieved without involving page reloads has become very popular for Web services. This can be handled in a variety of ways. This article covers several of them, starting with Asynchronous JavaScript and XML (AJAX).

Author Info:
By: Alejandro Gervasio
Rating: 5 stars5 stars5 stars5 stars5 stars / 39
September 21, 2005
TABLE OF CONTENTS:
  1. · JavaScript Remote Scripting: Reading Data From the Server
  2. · A non-standard approximation: reading server data with AJAX
  3. · Checking asynchronous http requests: taking a look at the "statusChecker()" function
  4. · Displaying file contents: defining the "createDataContainer()" and "displayData()" functions
  5. · Reusing the source code: listing the full script

print this article
SEARCH DEVARTICLES

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.

blog comments powered by Disqus
JAVASCRIPT ARTICLES

- Project Nashorn to Make Java, JavaScript Wor...
- JavaScript Virus Attacks Tumblr Blogs
- Google Releases Stable Dart Version, JavaScr...
- Khan Academy Unveils New JavaScript Learning...
- Accessing Nitro? There`s an App for That
- JQuery 2.0 Leaving Older IE Versions Behind
- Fastest JavaScript Engine Might Surprise You
- Microsoft Adjusting Chakra for IE 10
- Brendan Eich: We Don`t Need Google Native Cl...
- An Overview of JavaScript Statements
- An Overview of JavaScript Operators
- Overview of JavaScript Variables
- More of the Top jQuery Social Plugins
- The Top jQuery Social Plugins
- More of the Top jQuery Slider Plugins

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials