faqts : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

2 of 15 people (13%) answered Yes
Recently 1 of 10 people (10%) answered Yes


How do I dynamicaly change the alt attribute of image tag in NN4 ?

Apr 6th, 2008 19:37
ha mo, Colin Fraser, Sree Kumar,

Ok take the simplest form 
if (navigator.appName=="Netscape"){ 
    appn = "NN" }
else   {    if (navigator.appName=="Microsoft" {
       appn = "IE" ; }
This is not a function, to be called, it simply sets up variables that
identify the browser in the head so they can be used anywhere int he
document. It is the variables we can use to write the image tags like : 
if (appn == "NN) (
    document.write("<img src='browser.gif' alt='Netscape' >")}
else {
    if (appn == "IE" ) {
    document.write("<img src='browser.gif' alt='Microsoft' >")}
and this can be re-used anywhere in your page. Place the identifying
function in your *.js file and it can be used by any page. This code can
also be manipulated, or rather, adapted, as generic code and calls can
be placed in pages with the generic code in the *.js file by using a
line like : 
function writeImage(pathtoimage,alttext) {
    document.write("<img src='"+pathtoimage+"' alt='"+alttext+"'>")
in a standard function 
In the page you may want to write something like 
if (appn == "NN) ( 
    writeImage("images/thisimg1.gif","whatever text you want");
else {
    if (appn == "IE" ) {
    writeImage("images/thisimg1.gif","whatever other text you want");}
This is actually a lot simpler than you may think, so try it and I am
sure you can adapt the code to whatever you want..  
Of course, the major flaw is what if the browser is neither NN or IE? In
this case you may want to use a switch statement to write images and
their respective alt values to the document, setting the appn variable
to whatever you want.
I suspect however, the main problem is going to be the method you use to
determine what is going to be the call to whatever function you use to
write the alt tags. As an alternative, you may want to use an array to
store the alt text values, then call that value as you need it. 
The only other issue here is that it may may take a lot of extra coding
to get this to work as you want it. It may slow the load times of the
page down to unacceptable levels, so just be sure you want to do this.    
Good luck..