JavaScript
  Home arrow JavaScript arrow Page 3 - Dynamic Text Replacement with JavaScript
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 
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

Dynamic Text Replacement with JavaScript
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 6
    2007-09-19

    Table of Contents:
  • Dynamic Text Replacement with JavaScript
  • Using Dynamic Text Replacement in a concrete case
  • Seeing Dynamic Text Replacement in action
  • Completing the dynamic text-replacement script

  • 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


    Dynamic Text Replacement with JavaScript - Seeing Dynamic Text Replacement in action


    (Page 3 of 4 )

    As I stated in the section that you just read, to demonstrate how to replace the <h2> headers of the previous sample web page using the dynamic text replacement technique, I’ll define a JavaScript function to traverse the web document tree, find the mentioned headers, and finally substitute them with some basic images.

    Having explained how this brand new function will work, here is its signature:

    function replaceTextNodes(){
     
    // select all <h2> elements on the web page
     
    var h2=document.getElementsByTagName('h2');
     
    if(!h2){return};

      // perform text replacement
     
    for(var i=0;i<h2.length;i++){
        // create <img> element
       
    var img=document.createElement('img');
       
    if(h2[i]){

          // set <img> attributes
         
    img.setAttribute('src','img'+i+'.gif');
         
    img.setAttribute('width','180px');
         
    img.setAttribute('height','30px');
         
    img.setAttribute('alt',h2[i].firstChild.nodeValue);

          // replace text with image
         
    h2[i].replaceChild(img,h2[i].firstChild);
       
    }
     
    }
    }

    Indeed, the definition of the previous “replaceTextNodes()” JavaScript function is very comprehensive. As you can see this function loops over the complete web page tree, finds its <h2> headers, and then replaces their text nodes with some predefined images. Quite simple, right?

    Finally, the previous function should be invoked after the web document has been loaded on the browser, in the following way:

    window.onload=function(){
     
    if(document.createElement && document.getElementById &&
    document.getElementsByTagName){
       
    replaceTextNodes();
     
    }
    }

    All right, now that you know how the previous JavaScript function does its thing, let's assume that the images that will replace the text nodes corresponding to the <h2> headers of the sample web page can be created as follows:

    In that case, the web page displayed on the browser, after performing the aforementioned text replacement would look like this:

    In this case, I used some basic graphics to replace the text nodes included into all the <h2> headers of the previous sample web document, but as you surely know, this can be easily modified, and work with more polished images.

    So far, so good. At this point you hopefully grasped the programmatic logic that drives this text replacement approach, which can be quite useful when you need to substitute certain text-based elements of a given web document with some eye-catching graphics. However, if you’re anything like me, then possibly you’ll want to see how the complete text replacement application look, right?

    Thus, in the final section of this tutorial, I’ll be listing the full source code of the previous hands-on example. This means that you can use it and eventually incorporate it into your own improvements.

    Click on the link below and keep reading, please.

    More JavaScript Articles
    More By Alejandro Gervasio


       · As you may have heard, Dynamic Text Replacement is a powerful approach that allows...
       · Frequently users cut and paste from Word or some other word processing application...
       · Thank you for the comments on my article on using dynamic text replacement, and also...
       · Hi, how do search engines view this technique?It sounds bit like underhand tactics...
       · Thank you for the comments on my article on dynamic text replacement. Concerning...
     

    JAVASCRIPT ARTICLES

    - Validating Digits and Dates with jQuery`s Va...
    - Validating Ranges, Emails, and URLs with jQu...
    - More Uses for the jQuery Tooltip Plug-in`s b...
    - Building Image-Based Tooltips with the jQuer...
    - Using the jQuery Tooltip Plug-in`s bodyHandl...
    - Using Rangelength, Min and Max with the Vali...
    - Using Minlength and Maxlength with the Valid...
    - Modifying Tooltip Coordinates with the jQuer...
    - Applying a Fade Out Effect with the jQuery T...
    - Tracking Mouse Movements with the jQuery Too...
    - Checking Online Forms with the Validator jQu...
    - Nested JavaScript Functions as Objects
    - The jQuery Tooltip Plug-in
    - Active Client Pages at the Server
    - ACP Tab Web Page







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