Home arrow JavaScript arrow Page 4 - JavaScript Remote Scripting: An AJAX-based Random Code Generator in Action

JavaScript Remote Scripting: An AJAX-based Random Code Generator in Action

In this article, Alejandro Gervasio finishes his discussion of creating a random code generator. Going beyond the core functions, he defines the remaining JavaScript functions needed to turn the random code generator into a functional program.

Author Info:
By: Alejandro Gervasio
Rating: 5 stars5 stars5 stars5 stars5 stars / 15
October 26, 2005
  1. · JavaScript Remote Scripting: An AJAX-based Random Code Generator in Action
  2. · Looking at existing functions: the “sendRequest()” and “stateChecker()” functions at a glance
  3. · Displaying random codes: defining the “createDataContainer()” function
  4. · Dynamic link generation: taking a look at the “createLinks()” function
  5. · Server-side processing: generating four-digit random codes with PHP
  6. · Putting the pieces together: showing the application’s complete code

print this article

JavaScript Remote Scripting: An AJAX-based Random Code Generator in Action - Dynamic link generation: taking a look at the “createLinks()” function
(Page 4 of 6 )

The reason for coding this function is rather simple. I don’t want to include any rating links in the original structure of the web page, since they’ll be added through JavaScript, specifically with the DOM. Doing so, I’m making sure that users with scripting disabled won’t be disappointed if they ever try to rate a particular article. That’s why I decided to generate rating links by using the DOM. Before I show you the definition for this new function, I’d like to clarify one point: it’s possible to build the whole voting application completely without JavaScript. However, for this particular case, I’ll use a JavaScript-based approach so you can see how remote scripting is implemented by utilizing client-side programming.

Now, let’s pay attention to the “createLinks()” function, defined in the following way:

function createLinks(){
    var ps=document.getElementsByTagName('p');
    for(var i=0;i<ps.length;i++){
        // create <span> elements
        var sp=document.createElement('span');
        // create <a> elements
        var a=document.createElement('a');
        a.setAttribute('title','Rate this article!');
        a.appendChild(document.createTextNode('Rate this
        // assign 'onclick' event handler to <span> elements

By studying the function above, it’s clear what’s happening. In simple terms, the function iterates over all the paragraph elements of the web document (remember that heading texts of articles were wrapped into paragraphs), in order to append to each of them the corresponding rating links. Once links are inserted into the web page, turn your attention to the following piece of code:


As you can see, what I’m doing here is providing each link (wrapped into a <span> tag) with the ability to request the “process_input.php” file every time it is clicked on. Notice the call to the “sendRequest()” function, discussed in my previous article, which accepts the name of the file to be fetched, along with the link that generated the “click” event.

Now that you know how rating links are dynamically appended to the web document, it’s time to look at the processing performed on the server, for generating four-digit random codes. All you have to do is keep on reading.

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