Forum Moderators: open

Message Too Old, No Replies

getElementsByTagName onclick

         

webaster

10:36 am on Mar 10, 2005 (gmt 0)

10+ Year Member



I want to iterate all A tags and when a Opera user clicks on a link it receives the alert. And it has to be like this.

function replace(){

if(/opera/i.test(navigator.userAgent)) {
//do something when clicked on any link

alert("Opera detected");
}
}

function doIterate(){

if (document.getElementsByTagName)

x = document.getElementsByTagName('a');
else if (document.all)
x = document.all.tags('a');

for (var i=0;i<x.length;i++)
{
x.onclick = replace;
}
}

Bernard Marx

1:25 pm on Mar 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just replace:

x.onclick = replace;

with:

x[i].onclick = replace;

PS: If you're only reacting to Opera clicks, you might as well put the 'Opera detector' into the function, doIterate. That way you don't even bother adding onclicks to anchors on non-Opera browsers.

too much information

1:46 pm on Mar 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a quick question about this script...

in the IF statement:
if (document.getElementsByTagName)
...
else if (document.all)
...

generally which browsers will react to the getElementsByTagName and which will react to the document.all?

webaster

2:14 pm on Mar 10, 2005 (gmt 0)

10+ Year Member



Level 1 DOM document.getElementsByTagName
Explorer 4 document.all

webaster

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

10+ Year Member



in fact I want when visiting a page Y coming from page X and that page Y becomes the last entry in the history url list -that when going back (onclick a link) to the previous page X-that the page Y gets erased from history everytime

Like this?

function replace(){
var isIe = (navigator.appName == "Microsoft Internet Explorer");
if(/opera/i.test(navigator.userAgent) ¦¦ isIe) {

location.replace('previous_history_entry.html');

}
}

function doIterate(){

if (document.getElementsByTagName)

x = document.getElementsByTagName('a');
else if (document.all)
// IE4
x = document.all.tags('a');

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

x[i].onclick = replace;
}
}

Bernard Marx

11:06 pm on Mar 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



too much information:
generally which browsers will react to the getElementsByTagName and which will react to the document.all?

Quite a few browsers support document.all for reasons of backward compatibility (and to avoid being rejected by inconsiderate scripters). The question is really:
What browsers don't support getElementById?

NS 4 and IE4

You can get around this by repointing at the head of your script:


/* collections returned by 'all' are also accessible by parentheses */
if(!document.getElementById){
document.getElementById = document.all ¦¦ function(id){return this.layers(id)};
}

This will give reasonable support for document.getElementById for IE4, and basic support for NS4. Since all returns elements referenced by name too, don't use the same name on one element, as you have used as an id on another.

It's also worth noting that some versions of IE5 (including mine) return an empty collection for getElementsByTagName('*'). So, if you need all the elements contained in an element, it's worth remembering that, if IE5 support is required.

Even Mozilla silently supports document.all:
[mozillanews.org...]