JavaScript
  Home arrow JavaScript arrow Page 3 - Checking Browser Image Support for a Dynam...
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  
Moblin 
JMSL Numerical Library 
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

Checking Browser Image Support for a Dynamic Text Replacement JavaScript Application
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 7
    2007-09-25

    Table of Contents:
  • Checking Browser Image Support for a Dynamic Text Replacement JavaScript Application
  • Reviewing the previous text replacement application
  • Adding image support checking
  • Completing the text replacement application: source 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


    Checking Browser Image Support for a Dynamic Text Replacement JavaScript Application - Adding image support checking


    (Page 3 of 4 )

    As you'll surely know, there are many ways to determine whether or not a specific browser has support for images. Solutions to this issue may range from using simple JavaScript snippets to implementing more complex CSS hacks.

    In this case, however, I'm going to utilize a neat and efficient technique developed originally by Peter-Paul Koch (you can read more about this technique here) to provide the previous text replacement script with the ability to check the browser for image support.

    Having clarified that point, here's how the improved text replacement script now looks:

    function replaceTextNodes(elemName){

        var testImg=new Image();

         testImg.src='test.gif';

         testImg.onload=function(){

     // select all <elemName> elements on the web page

        var elems=document.getElementsByTagName(elemName);

          if(!elems){return};

     // perform text replacement

          for(var i=0;i<elems.length;i++){

     // create <img> element

        var img=document.createElement('img');

           if(elems[i]){

     // set <img> attributes

           img.setAttribute('src','img'+i+'.gif');

           img.setAttribute('width','180px');

           img.setAttribute('height','30px');

           img.setAttribute('alt',elems[i].firstChild.nodeValue);

     // replace text by image

            elems[i].replaceChild(img,elems[i].firstChild);

        }

       }

     }

    }

    As you can see, the above "replaceTextNodes()" function has the ability to determine whether or not the browser supports images via the use of a tiny 1px X 1px transparent image called "test.gif." Successful loading of the text image fires up the execution of the rest of the text replacement script. Otherwise, the function simply will do nothing.

    In addition, the modified function could be triggered after the web page has been loaded, in the following way:

    window.onload=function(){

        if(document.createElement&&document.getElementById&&document.
    getElementsByTagName){

      replaceTextNodes('h2');

      }

    }

    Actually, the modification that I made to the previous JavaScript function is completely optional, but it doesn't hurt to add some image support checking capabilities. Of course, the final decision regarding the inclusion of this improvement is entirely up to you.

    Well, at this point I should assume that you already grasped the technique for determining whether or not support for images has been enabled on the browser. So it's time to include the improved "replaceTextNodes()" JavaScript function into the same (X)HTML file that contains the structural markup and CSS styles of the sample web page that you saw before.

    In the next section I will show you the full source code for the text replacement application. This will make it easier to put it into a single source file. You will also have an easier time testing the source code and studying it to make your own modifications. So please, click on the link that appears below and read the last section of this tutorial.

    More JavaScript Articles
    More By Alejandro Gervasio


       · In this final tutorial of the series, you’ll learn how to incorporate image support...
     

    JAVASCRIPT ARTICLES

    - 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
    - Ajax Hack for Entering Information Without R...
    - EXT JS 2.1 Overview
    - Using the Style Object for Zebra Tables with...
    - Binary Searching
    - An Improved Approach to Building Zebra Tables






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