Forum Moderators: open

Message Too Old, No Replies

document.getElementsById

document.getElementsById

         

webaster

4:24 pm on Mar 10, 2005 (gmt 0)

10+ Year Member



I use a dom_events library js too - to use addEvent accordingly

Is it necessary to include document.all[id] to supoport IE4
and furthermore is there need of braces when using if, if else or is used correct here and is the function well written thus correct syntax?

function replacer(){
var isIe = (navigator.appName == "Microsoft Internet Explorer");
var isOpera = (/opera/i.test(navigator.userAgent));
if(isOpera ¦¦ isIe) {
alert("ie or Opera");
self.location.replace("thispage.html");

}}

function doIterate(){

if (document.getElementsById)

x = document.getElementsById('backToThumbs');

else if (document.all)
// IE4
x = document.all['backToThumbs'];

x.onclick = replacer;
}

addEvent (window,'load',doIterate)

BlobFisk

5:09 pm on Mar 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi webaster,

I think you are referring to document.getElementById? Since ID's must be unique there can only be one per document, so the element is singular!

There is however a getElementsByTagName which allows you to build an array of the elements of a certain tag.

To answer your question, you do need to use document.all to support IE4... You can get away without using braces, but I think that the use of them makes for cleaner code...

HTH

webaster

5:32 pm on Mar 10, 2005 (gmt 0)

10+ Year Member



function replacer(){
var isIe = (navigator.appName == "Microsoft Internet Explorer");
var isOpera = (/opera/i.test(navigator.userAgent));
if(isOpera ¦¦ isIe) {
alert("ie or Opera");
self.location.replace("thispage.html");

}}

function doIterate(){

if (document.getElementById)

x = document.getElementById('backToThumbs');

else if (document.all)
// IE4
x = document.all['backToThumbs'];

x.onclick = replacer;
}

addEvent (window,'load',doIterate)

webaster

5:35 pm on Mar 10, 2005 (gmt 0)

10+ Year Member



x = document.getElementById('backToThumbs');

should the var be set to null at the end of the function (to save a little bandwidth)

unction doIterate(){

if (document.getElementById)

x = document.getElementById('backToThumbs');

else if (document.all)
// IE4
x = document.all['backToThumbs'];

x.onclick = replacer;

x= null;
}

addEvent (window,'load',doIterate)

webaster

5:37 pm on Mar 10, 2005 (gmt 0)

10+ Year Member



eratta

function doIterate(){

...

Bernard Marx

10:35 pm on Mar 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If x is made into a local variable, which it should be, it will 'expire' when the function returns, so there's no need to set it to null.