...

View Full Version : Different link on image depending on browser



guru84
04-26-2008, 04:48 AM
Hello,
I have an image that is a link and I want the same image to have a different destination when clicked depending on the users browser. If they are using Firefox I want them to go to one specific link and if not I want them to go to another. I was able to do this with a text link, but I can't do it with an image. This is what works for the text with the link text of "TEXT" but how do I replace the link text with an image? What I have so far is:
<SCRIPT language="JavaScript">
<!--
var browserName=navigator.appName;
var str="TEXT";
{
if (browserName=="Netscape")
{
document.write(str.link("/one.html"));
}
else
{
document.write(str.link("two.html"));
}
}
//-->
</SCRIPT>

Thank you so much for your help

Kor
04-26-2008, 07:50 AM
May I ask you why do you need that? Usually the developers try to do a single site/document visible the same in all the browsers.

Philip M
04-26-2008, 09:53 AM
I agree with Kor, but .....



<script type = "text/javascript">

function redirect() {
var browserName = navigator.appName;
if (browserName == "Netscape"){
window.location = "/one.html";
}
else {
window.location = "/two.html";
}
}

</script>


<img src = "hog.gif" onclick ="redirect()">



BTW, the time to say "thanks" is afterwards, not beforehand which gives the impression that you take other people's assistance for granted. Or as British politician Neil Kinnock put it, "Don't belch before you have had the meal." Prefer to use "please" beforehand and if you find a response helpful then you can use the "Thank You For This Post" button.

Kor
04-26-2008, 10:18 AM
When I put that question I thought that, maybe, guru84 need that to make a sort of tutorial in examples how act some browsers in different situations, or something like that. In any other cases detecting the browser by appName is useless, even confusing (for instance some browsers pretend to bear other names - ex: Opera -> IE...).

There are cases when you need, for a crossbrowser code, to write separate codes for separate browsers, but in fact what is important is not the browser itself (which might behave in different manners, according to the version - ex: IE5 - IE6 - IE7, or NS4 - NS6 - NS7) but the javascript method/object which can be applied.

By short, don't detect the browser, detect the method/object

A classical example to refer elements:


if(document.getElementById){//modern browsers; IE5, 6, 7, 8 will enter here
}
else if(document.all){//IE4
}
else if(document.layers){//NS4
}


There are as many crossbrowser codes as differences between methods/objects, not as browsers' type.

As a note, Netscape is almost dead and buried. I see no use in detecting it. Even so, which Netscape? 4, 6, 7, 8?

What is so special in your page that you need a duplicate for Netscape only?

abduraooft
04-26-2008, 10:29 AM
Obviously the flow of this thread depends on the next post of guru84, but if he really want to proceed with this idea, then a serverside sniffing would be more practical, assuming he has some serverside script support. :)

Philip M
04-26-2008, 10:36 AM
For some unfathomable reason the OP wants to detect the browser. If the browser is Netscape he wishes to redirect to a certain page, if not (default) to another page.

Perhaps he wants to show a message saying "Netscape is now virtually obsolete - get yourself MSIE 7" or something.

The deprecated <SCRIPT language="JavaScript"> gives a clue.

Kor
04-26-2008, 11:56 AM
For some unfathomable reason the OP wants to detect the browser.
I doubt about it. Most of the unexperienced coders only think that they need to detect the browser to redirect the location, because they have no idea that they might detect the method instead, or even make a single crossbrowser code for a single page. Let's find out the real reason for guru84 has two pages, one for Netscape, one for the others.

guru84
04-26-2008, 01:13 PM
Thanks everyone for your ideas. The reason that I want this is because an advertiser on my site advertises an add-on for Firefox and doesn't want to waste impressions on other browsers. For some reason targeting to Firefox only works when the name "Netscape" is used. Just because thats the way things have worked out I therefore have 2 seperate pages, depending on the visitors browser. I am yet to try the ideas above but I will give them a go and let you know how they work.
Thanks again.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum