JavaScript
  Home arrow JavaScript arrow Page 2 - 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 - One step toward standardization: making http requests with the DOM


    (Page 2 of 5 )

    In order to demonstrate how to use standard DOM constructs to implement remote scripting, my trusty example will be similar to the script you saw in my previous article. Essentially, I’ll build an extensible JavaScript program, which will be able to request an XML file to the server. This in turn will parse this file and send the data back to the client in the form of standard-compliant JavaScript code. Once the parsed data has been received, it will be displayed through a headlines sequencing mechanism.

    Even when the programming logic above described is quite simple, the example will be helpful for understanding how to handle http requests without the need to look at AJAX. Considering this possibility, I’ll define the first function of the script, “sendRequest()”, which accepts the name of the file to be requested and fetches the file by using only a few DOM methods. Its definition is as follows:

    function sendRequest(file){
        // create <script> element
        var js=document.createElement('script');
        // assign <script> attributes
        js.setAttribute('language','javascript');
        js.setAttribute
    ('src','http://www.mydomain.com/scripts/script_file.php?
    file='+file);
        // append element to document tree & send GET request
        document.getElementsByTagName('head')[0].appendChild(js);
    }

    As I explained before, the above function only uses standard DOM methods to request a given file. It takes the file name as an argument and builds a <script> node on the fly, by specifying a couple of attributes for the element just created. Just take a look at the line below:

    js.setAttribute
    ('src','http://www.mydomain.com/scripts/script_file.php?
    file='+file);

    Once the <script> node has been appended to the document tree, this statement is really instructing the browser to request the URL specified as the value for the “src” attribute. As a result, if I’d pass in as an argument a “news.xml” file, like this:

    'http://www.mydomain.com/scripts/script_file.php?file=news.xml

    the specified file would be requested, and incidentally all the code –- server and client statements -- would also be executed. On the plus side, the above method eliminates the limitations imposed by AJAX, since it allows you to silently request files from any domain, while AJAX will only fetch documents from the originating server (except when the script is run as localhost).

    By way of returning to the pertinent function, that’s all the required JavaScript code to fetch files from the server through the DOM, without being restricted by the security model implemented on most browsers. So, the next step is to study the code that will be run on the requested file “script_file.php”, in order to define the remaining JavaScript functions, along with some PHP XML parsing statements.

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