Home arrow JavaScript arrow Page 3 - JavaScript Remote Scripting: Building an AJAX-based Random Code Generator

JavaScript Remote Scripting: Building an AJAX-based Random Code Generator

This article explains how to build a random code generator, specifically applied to a voting system. This will help you to avoid system hacks involving automated submissions.

Author Info:
By: Alejandro Gervasio
Rating: 4 stars4 stars4 stars4 stars4 stars / 14
October 19, 2005
  1. · JavaScript Remote Scripting: Building an AJAX-based Random Code Generator
  2. · Protecting against automated data submission: the basics of a random data generator
  3. · Implementing the core logic of the script: defining the “statusChecker()” function
  4. · Avoiding simultaneous data submission: coding the “removeActiveNodes()” function

print this article

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 status == 200 display text file
            // remove active nodes
            // create data container & display challenge value
            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.

blog comments powered by Disqus

- 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 

Developer Shed Affiliates


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