spacer
Yehuda Shiran January 24, 2001
Extending getSrc() to Mozilla Browsers
Tips: January 2001

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs

DOCJSLIB 1.0 includes three functions:

  • makeImage() creates an IMG element within a DIV element.
  • getSrc() returns the image's URL.
  • setSrc() sets the image's URL.

To adapt getSrc() to Netscape 6 and Internet Explorer 5.x, we need to use document.getElementByID instead of document.all and document.id. Here is the old version:

function getSrc(id) {
  if (NS4) {return eval("document." + id + ".document." + id + "img.src")}
  else {return eval("document.all." + id + "img.src")}
}

and here is the new version that supports both Netscape 6 and IE5.x:

function getSrc(id) {
  if (NS4) {return eval("document." + id + ".document." + id + "img.src")}
  else if (IE4) {return eval("document.all." + id + "img.src")}
       else if (IE5 || N6) {return document.getElementById(id + "img").src};
}

The convention used in DOCJSLIB is that if a DIV element is labeled with a given ID, the ID of the IMG element within the DIV element is a concatenation of ID and the string "img". If the DIV's ID is "foo", for example, the IMG element within it will be labeled "fooimg". This is the reason we search above an IMG element that has an ID of id + "img".

The different browsers are detected via the following code:

var IE4 = (document.all && !document.getElementById) ? true : false;
var NS4 = (document.layers) ? true : false;
var IE5 = (document.all && document.getElementById) ? true : false;
var N6 = (document.getElementById && !document.all) ? true : false;


People who read this tip also read these tips:

Look for similar tips by subject:

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint