JavaScript
  Home arrow JavaScript arrow Page 3 - JavaScript Remote Scripting: Building an A...
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

JavaScript Remote Scripting: Building an AJAX-based Random Code Generator
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 14
    2005-10-19

    Table of Contents:
  • JavaScript Remote Scripting: Building an AJAX-based Random Code Generator
  • Protecting against automated data submission: the basics of a random data generator
  • Implementing the core logic of the script: defining the “statusChecker()” function
  • Avoiding simultaneous data submission: coding the “removeActiveNodes()” function

  • 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: Building an AJAX-based Random Code Generator - Implementing the core logic of the script: defining the “statusChecker()” function


    (Page 3 of 4 )

    Surely, you may have noticed that once an XMLHttpRequest object has been instantiated, the “stateChecker()” function is tied to the “onreadystatechange” event handler, in order to verify the progress of an http request. Very similar in its definition to previous examples, this function is defined as follows:

    function stateChecker(elem){
        // if request is completed
        if(xmlobj.readyState==4){
            // if status == 200 display text file
            if(xmlobj.status==200){
                // remove active nodes
                removeActiveNodes();
                // create data container & display challenge value
                createDataContainer(elem,xmlobj.responseText);
            }
            else{
                alert('Failed to get response :'+ xmlobj.statusText);
            }
        }
    }

    As you can see, the above code snippet speaks for itself. After the request has been successfully completed, that is the “readyState” property has a value of “200”, the function calls in sequence to the “removeActiveNodes()” and “createDataContainer()” functions. Don't be concerned about them now, since they’ll be explained shortly.

    For the moment, I’ll say that the first function is tasked with removing from the document tree any input box that might be active when a user clicks on a rating link, while the second function is responsible for building the corresponding input boxes, as well as displaying the random codes that you saw right at the second screenshot.

    By returning to the flow of the script, there are some additional points that should be noticed with reference to the “stateChecker()” function. First, despite its extreme simplicity, it really implements the driving logic of the program, because each request is made in asynchronous mode. By delegating program control to a function that checks for the progress of http requests, it’s fairly easy to build the overall programming logic around this function, without the need to implement more complex control blocks.

    At this point, I’ve explained in detail the programming logic of the whole application, as well as the tasks of some relevant program functions, such as “sendRequest()” and “stateChecker()”. Keeping this concept in mind, the next step will be taking a look at the “removeActiveNodes()” function, so you can see how the program avoids the circumstance of having an user trying to rate multiple articles through the same request.

    More JavaScript Articles
    More By Alejandro Gervasio


       · In this tutorial, I explore the implementation of a JavaScript application for...
     

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