JavaScript
  Home arrow JavaScript arrow Page 4 - JavaScript Remote Scripting: An AJAX-based...
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: An AJAX-based Random Code Generator in Action
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 12
    2005-10-26

    Table of Contents:
  • JavaScript Remote Scripting: An AJAX-based Random Code Generator in Action
  • Looking at existing functions: the “sendRequest()” and “stateChecker()” functions at a glance
  • Displaying random codes: defining the “createDataContainer()” function
  • Dynamic link generation: taking a look at the “createLinks()” function
  • Server-side processing: generating four-digit random codes with PHP
  • Putting the pieces together: showing the application’s complete 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


    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');
        if(!ps){return};
        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('href','#');
            a.setAttribute('title','Rate this article!');
            a.appendChild(document.createTextNode('Rate this
    article!'));
            sp.appendChild(a);
            // assign 'onclick' event handler to <span> elements
            sp.onclick=function(){
                sendRequest(this,'process_input.php');
            }
            ps[i].appendChild(sp);
        }
    }

    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:

    sp.onclick=function(){
        sendRequest(this,'process_input.php');
    }

    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.

    More JavaScript Articles
    More By Alejandro Gervasio


       · Over the last part of the series, the remaining JavaScript functions that comprise...
     

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