JavaScript
  Home arrow JavaScript arrow Page 2 - Handling Multiple Web Page Elements with D...
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

Handling Multiple Web Page Elements with Dynamic Text Replacement with JavaScript
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2007-09-24

    Table of Contents:
  • Handling Multiple Web Page Elements with Dynamic Text Replacement with JavaScript
  • The complete source code of the original text replacement application
  • Replacing multiple web page elements
  • Putting the full text replacement application into a single file

  • 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


    Handling Multiple Web Page Elements with Dynamic Text Replacement with JavaScript - The complete source code of the original text replacement application


    (Page 2 of 4 )

    In accordance with the concepts deployed in the introduction of this article, my plan basically consists of expanding the original functionality of the JavaScript application that I built previously. As I mentioned, I want to give it the ability to replace any element of a specific web page, not just <h2> headers. But first I'd like to list the complete source code that corresponds to the original application, to help refresh your memory of how it looked. 

    Having said that, here's the complete code for the original text replacement script:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-
    8859-1" />
    <title>Image Replacement Example 2</title>
    <script language="javascript">
    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);
        }
      }
    }
    window.onload=function(){
      if(document.createElement && document.getElementById &&
    document.getElementsByTagName){
        replaceTextNodes();
      }
    }
    </script>
    <style type="text/css">

    body{
      padding: 0;
      margin: 0;
      background: #039;
    }

    h2{
      margin: 0;
      font: bold 18px Arial, Helvetica, sans-serif;
      color: #000;      
    }

    p{
      font: normal 12px Arial, Helvetica, sans-serif;
      color: #000;      
    }

    #header{
      width: 780px;
      padding: 10px;
      margin-left: auto;
      margin-right: auto;
      background: #ffc;
    }

    #navbar{
      width: 780px;
      padding: 10px;
      margin-left: auto;
      margin-right: auto;
      background: #fc0;
    }

    #navbar ul{
      list-style: none;
    }

    #navbar li{
      display: inline;
      padding-right: 4%;
    }

    #navbar a:link,#navbar a:visited{
      font: normal 12px Arial, Helvetica, sans-serif;
      color: #039;
      text-decoration: none;
    }

    #navbar a:hover{
      text-decoration: underline;
    }

    #mainwrapper{
      clear: both;
      width: 800px;
      height: 100%;
      margin-left: auto;
      margin-right: auto;
      overflow: hidden;
      background: #eee;
    }

    #mainwrapper .leftcol{
      position: relative;
      float: left;
    }

    #mainwrapper .rightcol{
      position: relative;
      float: right;
    }

    #leftbar{
      width: 180px;
      padding: 10px;
    }

    #centerbar{
      float: left;
      width: 380px;
      padding: 10px;
      background: #fff;
    }

    #rightbar{
      width: 180px;
      padding: 10px;
    }

    #footer{
      clear: both;
      width: 780px;
      padding: 10px;
      margin-left: auto;
      margin-right: auto;
      background: #ffc;
    }

    </style>
    </head>
    <body>
      <div id="header">
        <h2>This is the header section of the web page</h2>
       
    <p>Contents for header section go here. Contents for header
    section go here. Contents for header section go here. Contents
    for header section go here.</p>
      </div>
     
    <div id="navbar">
       
    <h2>This is the navigation bar of the web page</h2>
       
    <ul>
         
    <li><a href="#">Link 1</a></li>
         
    <li><a href="#">Link 2</a></li>
         
    <li><a href="#">Link 3</a></li>
         
    <li><a href="#">Link 4</a></li>
         
    <li><a href="#">Link 5</a></li>
         
    <li><a href="#">Link 6</a></li>
       
    </ul>
     
    </div>
     
    <div id="mainwrapper">
       
    <div id="leftbar" class="leftcol">
         
    <h2>This is the left column of the web page</h2>
         
    <p>Contents for left column go here. Contents for left
    column go here. Contents for left column go here. Contents for
    left column go here. Contents for left column go here. Contents
    for left column go here. Contents for left column go here.
    Contents for left column go here. Contents for left column go
    here. Contents for left column go here.</p>
       
    </div>
       
    <div id="centerbar" class="leftcol">
         
    <h2>This is the center column of the web page</h2>
         
    <p>Contents for center column go here. Contents for center
    column go here. Contents for center column go here. Contents for
    center column go here. Contents for center column go here.
    Contents for center column go here. Contents for center column go
    here. Contents for center column go here. Contents for center
    column go here. Contents for center column go here.</p>
         
    <p>Contents for center column go here. Contents for center
    column go here. Contents for center column go here. Contents for
    center column go here. Contents for center column go here.
    Contents for center column go here. Contents for center column go
    here. Contents for center column go here. Contents for center
    column go here. Contents for center column go here.</p>
         
    <p>Contents for center column go here. Contents for center
    column go here. Contents for center column go here. Contents for
    center column go here. Contents for center column go here.
    Contents for center column go here. Contents for center column go
    here. Contents for center column go here. Contents for center
    column go here. Contents for center column go here.</p>
       
    </div>
       
    <div id="rightbar" class="rightcol">
         
    <h2>This is the right column of the web page</h2>
         
    <p>Contents for right column go here. Contents for right
    column go here. Contents for right column go here. Contents for
    right column go here. Contents for right column go here.</p>
       
    </div>
     
    </div>
     
    <div id="footer">
       
    <h2>This is the footer section of the web page</h2>
       
    <p>Contents for footer section go here. Contents for footer
    section go here. Contents for footer section go here. Contents
    for footer section go here. Contents for footer section go
    here.</p>
     
    </div>
    </body>
    </html>

    As shown above, the "replaceTextNodes()" JavaScript function does precisely that on the sample web page listed previously. In this case, as I explained before, the function performs the replacement on all the text nodes that correspond to the <h2> headers contained into the web document. This makes it possible to display headers that are image-based rather than text-based.

    However, as you can see, the function's functionality is rather limited, since it only targets <h2> elements. Thus, it'd be highly desirable to extend its capacity to provide it with the ability to replace any element of a selected web page.

    In the next section I'll show you how to improve the signature of the original text replacement script so it can target all the elements contained into a given web document. To learn how this improvement will be introduced into the initial application, please go ahead and read the next few lines.

    More JavaScript Articles
    More By Alejandro Gervasio


       · In this second tutorial of the series, the signature corresponding to the JavaScript...
       · Yeah this is great, but how does one do the most useful feature of text replacement,...
     

    JAVASCRIPT ARTICLES

    - More on JavaScript Array Objects
    - Methods of the DOM Location Object
    - The DOM Location Object Properties
    - Handling Remote Files with JavaScript Click ...
    - 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...


     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     





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