Generating object pointers automatically

Object pointers (see previous page) let you use IE-style direct references in Netscape Navigator. Navigator is quite happy for you to create a pointer with the same name as the tag's name= attribute, like this:

<img name="myImg" src="pic0.jpg">

<script>
myImg = document.images["myImg"]
</script>

However Internet Explorer will generate an error if you do this, because it will already have created an alias based on the name= value. The answer is to prevent the pointer-generating code from executing under IE. Here's a routine that generates same-name pointers for all the image objects in the document.images[] collection:

function hookup() {
if (!RunningIE4) {
  for (i in document.images) {
     xname = document.images[i].name
     eval(xname + ' = document.images["'+xname+'"]')
} }

Once this routine has run, you can use the image objects' name= values as direct identifiers, irrespective of which browser the page is running in. Here's an example:

<input type="button" value="Picture 1" onclick='myImg.changePic("pic1.jpg")'>

If it's IE, then "myImg" works because it's the alias automatically created by the browser. If it's Navigator, "myImg" works because it's the pointer that's just been created.