Click to See Complete Forum and Search --> : png and gif switch


Wildcat
09-06-2003, 02:07 PM
Hi, I'm trying to fix my website to display a .png logo whenever the browser can handle the transparency. The background colors of my site change, so rather than upload multiple versions, I decided to write a little script for it. I have found IE does not display the transparency correctly, but Opera does, and this is where I am running into a problem. I can't figure out how to get Opera to register as Opera to my script instead of IE. I will be grateful for any help you can give me. The script is already configured for Netscape, but if you see anything wrong there (I do not have access to NN 4.x to actually test in that browser) please tell me.

if (navigator.appName == 'Netscape') {
if (document.layers) {
document.write('<img src="starlogo.gif" alt="Shooting Stars Logo" \>');
}
else {
document.write('<img src="starlogo.png" alt="Shooting Stars Logo" \>');
} }
else {
document.write('<img src="starlogo.gif" alt="Shooting Stars Logo" \>'); }

Khalid Ali
09-06-2003, 02:12 PM
use the following


var isNS6 = (navigator.userAgent.indexOf("Mozilla")>-1 && navigator.userAgent.indexOf("Gecko")>-1)?true:false;
var isIE = (navigator.userAgent.indexOf("MSIE")>-1 && navigator.userAgent.indexOf("Opera")==-1)?true:false;
var isOp = (navigator.userAgent.indexOf("Opera")>-1)?true:false;

where

if(isOp){
alert("opera browser");
}

Charles
09-06-2003, 03:19 PM
Another, JavaScript free, method is to take advantage of the fact that MSIE can't handle the OBJECT element. The OBJECT (http://www.w3.org/TR/html4/struct/objects.html#edef-OBJECT) element was introduced back in 1997 and they still cannot get it right.

<object data="../images/separator640.png" type="image/png"><img alt="" src="../images/separator640.gif"></object>

Fang
09-06-2003, 03:22 PM
Does not the browser pick up the best supported image type?
The W3C use this in their link:
<img src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0!" height="31" width="88" />
In the Icons folder are valid-xhtml10.gif and valid-xhtml10.png If the browser can handle a png it will use it.

Charles
09-06-2003, 03:32 PM
The problem is that MSIE does recognize PNG format. It just gets is wrong.

Wildcat
09-06-2003, 04:26 PM
Thank you for the help! Khalid Ali, I put the code within my script, but when I opened the page in Opera, I wasn't getting the alert window. Charles, the object tag worked very well to get Netscape and Opera to display the .png over the gif, but IE won't display anything. Is there a way I can force it to display the <img> if I leave it between <object></object>?

Charles
09-06-2003, 04:57 PM
It's supposed to do just that and it does with most versions of MSIE. I did notice that it stopped working for me recently and after I installed the most recent patch. I had thought that it was something wrong with my instance but I guess that they decided to make that piece of dung even more dung-like.

Wildcat
09-06-2003, 09:07 PM
Thanks again to everyone. I think I found a suitable way to modify the script so it works. (Thanks especially to Khalid Ali for teaching me the userAgent part!)