JavaScript
  Home arrow JavaScript arrow Page 4 - JavaScript Remote Scripting: Fetching Serv...
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: Fetching Server Data with the DOM
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 7
    2005-10-12

    Table of Contents:
  • JavaScript Remote Scripting: Fetching Server Data with the DOM
  • One step toward standardization: making http requests with the DOM
  • Setting up the server response: parsing XML in the server
  • Displaying XML data: defining the “createDataContainer()” and “displayData()” functions
  • Putting the pieces together: showing the complete 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: Fetching Server Data with the DOM - Displaying XML data: defining the “createDataContainer()” and “displayData()” functions


    (Page 4 of 5 )

    So far, I’ve defined the functions responsible for fetching files from the server, together with a basic XML parser. Since all the XML nodes now reside in JavaScript arrays, it’s extremely easy to write a couple of functions that process and display the data. Bearing this concept in mind, I’ll code a simple function that creates a <div> containing element, handy for using as a headline wrapper. Given that, here’s the code for the “createDataContainer()” function:

    function createDataContainer(){
        var div=document.getElementById('container');
        if(div){return};
        var div=document.createElement('div');
        div.setAttribute('id','container');
        document.getElementsByTagName('body')[0].appendChild(div);
    }

    If you’ve read my previous article belonging to this tutorial series, the above function should be pretty familiar to you. Essentially it performs the same operation, by creating a <div> element and appending it to the document tree. This set of tags will behave as a simple wrapping structure for displaying the headlines.

    Now that you’ve seen how a data container is inserted within the document structure, the next thing to do is define another function, focused primarily on displaying the headlines in question. Below is the list of the “displayData()” function, tasked with showing the parsed XML nodes:

    function displayData(){
        // reset data container
        document.getElementById('container').innerHTML='';
        var ul=document.createElement('ul');
        document.getElementById('container').appendChild(ul);
        for(var i=0;i<URL.length;i++){
            // create links
            var li=document.createElement('li');
            var a=document.createElement('a');
            // assign 'href' attribute
            a.setAttribute('href',URL[i]);
            // add link labels
            a.appendChild(document.createTextNode(TITLE[i]));
            li.appendChild(a);
            ul.appendChild(li);
        }
        // remove <script> node
        var js=document.getElementsByTagName('script')[0];
        js.parentNode.removeChild(js);
        // reset array values
        URL=null;
        TITLE=null;
        // update headlines each 1 hour
        setTimeout("sendRequest('news.xml')",3600*1000);
    }

    Studying the function depicted above, there are some points worth mentioning. First, headlines are displayed within an (X)HTML list of links, where each of them points to the corresponding URL. Since the values of <title> and <url> nodes were stored in their respective arrays, the following lines:

    a.setAttribute('href',URL[i]);
    a.appendChild(document.createTextNode(TITLE[i]));

    build the appropriate links, by assigning the values of the URL array to the "href" property, as well as generating the labels with the elements of the TITLE array. After creating the links, they’re appended to the document, like this:

    li.appendChild(a);
    ul.appendChild(li);

    The rest of the function code is quite straightforward. Since I want the headlines to be updated at a specific time interval, I simply remove the <script> node previously created and reset both the URL and TITLE arrays. Finally, in order to implement a way for automatically updating the headlines, the “sendRequest()” function is wrapped up into a “setTimeout()” method:

    setTimeout("sendRequest('news.xml')",3600*1000);

    Of course, this last addition can be easily modified in case you don’t want to have a JavaScript timer running silently within your application.

    Having demonstrated how XML files are parsed and displayed by using only standard DOM methods, the next step consists of listing the complete source code for the script, so you can study it and introduce your own modifications.

    More JavaScript Articles
    More By Alejandro Gervasio


       · Over this article, find out how to make silent http requests, by using only the DOM....
       · Hi, I thought it is a good article to understand how to use javascript to...
       · Hello,I'm glad to know this article has been useful to you. Now, comming to your...
       · Hi, Alejandro,I found your article really thorough and easy-to-understand. Thank...
       · Hi friend,Thanks a lot for the kind comments on my JavaScript article. I found...
       · Hi,This article is very helpful for me.But I am facing difficulties in running...
       · Thank you for commenting on my JavaScript article, and I’m glad it’s been useful to...
       · Hi,Thanks once again for the help.I have all the applications installed in my...
       · Thank you for commenting on my JavaScript article. Here are some basic steps to...
     

    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 2 Hosted by Hostway
    Stay green...Green IT