Forum Moderators: open
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;
}
}
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;
}
}
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...]