JavaScript
  Home arrow JavaScript arrow Page 3 - Building the Behavioral Layer for a Networ...
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 
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

Building the Behavioral Layer for a Network Processor with AJAX
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2006-12-27

    Table of Contents:
  • Building the Behavioral Layer for a Network Processor with AJAX
  • Listing the previous client-side code of the networking application
  • Querying an Internet host in the background
  • Turning query buttons into functional controls
  • Listing the full client-side code

  • 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


    Building the Behavioral Layer for a Network Processor with AJAX - Querying an Internet host in the background


    (Page 3 of 5 )

    As you may have guessed, to query a selected Internet host in the background it’s necessary to define a JavaScript function that allows the use of an HTTP requester object. In this way the corresponding requests can be adequately processed by the web server on its side.

    Of course, this task will be performed by the function below, which I named “sendHttpRequest().” Its signature is as follows:

    // send http requests

    function sendHttpRequest(url,callbackFunc,respXml){

    var xmlobj=null;

                try{

                            xmlobj=new XMLHttpRequest();

                }

                catch(e){

                            try{

                                                   xmlobj=new
    ActiveXObject("Microsoft.XMLHTTP");

                            }

                            catch(e){

                                                   alert('AJAX is not
    supported by your browser!');

                                                   return false;

                            }

                }

                xmlobj.onreadystatechange=function(){

                            if(xmlobj.readyState==4){

                                                   if
    (xmlobj.status==200){

                                                   respXml?eval
    (callbackFunc+'(xmlobj.responseXML)'):eval
    (callbackFunc+'(xmlobj.responseText)');

                                                   }

                            }

                }

                // open socket connection

                xmlobj.open('GET',url,true);

                // send http header

                xmlobj.setRequestHeader('Content-Type','text/html;
    charset=UTF-8');

                // send http request

                xmlobj.send(null);

    }

    In this particular case, the above function will send the mentioned HTTP requests via an asynchronous call, using the “GET” method. Additionally, each time a request is triggered, a command variable will be passed via the query string, which will be processed accordingly by the server.

    Logically, the reason for using the mentioned variable is simply to provide the server with the ability to determine what type of query must be executed. But I'm getting ahead of myself. Let me show you another relevant JavaScript function, in this case tasked with displaying the results of an eventual query. Its short definition is as follows:

    // display command results

    function displayCommandResults(results){

    var centpanel=document.getElementById('centerpanel');

                if(!centpanel){return};

                centpanel.innerHTML='';

                centpanel.innerHTML=results;

    }

    As you can see, the previous JavaScript function simply returns the results to the client, once the corresponding query has been executed in the server, in this case using the “responseText” property that belongs to the HTTP requester object in use. For this purpose, the function in question uses the DIV element identified as “centerpanel,” which was discussed adequately in the first part of the series. So far, nothing too hard to grasp, right?

    At this point, I have shown you the first pair of JavaScript functions that comprise the behavioral layer of this AJAX-powered networking application. So what’s next? Well, as you’ll recall, I mentioned that each time an HTTP request is sent, a command variable is passed straight to the web server.

    In response to this situation, I need to define another brand new function that not only creates the command variable that I referenced before, but also assigns “onclick” event handlers to all the query buttons included in the corresponding front-end, developed in the preceding article.

    To learn how all these tasks will be carried out, please jump ahead and read the following section.

    More JavaScript Articles
    More By Alejandro Gervasio


       · Over this second article of the series, the complete set of JavaScript functions...
     

    JAVASCRIPT ARTICLES

    - Using Click Interceptions with a Database-Dr...
    - Using JavaScript Click Interceptions in an I...
    - Using Click Interceptions with JavaScript
    - QuickSort in Action
    - Quicksort
    - 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






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