How to detect a browser that dosen't render .png transparency

孤者浪人 提交于 2019-12-05 21:16:40

I've done a reasonable amount of research into this this issue and came to the conclusion that it was not feasible to create a feature test in JavaScript, and that IE 6 is the only browser in serious use that doesn't support PNG transparency. That being the case, your best bet is conditional comments, as recommended by Gabriel Ross. It is possible to use conditional comments in pure JavaScript, which I think is what you want:

var div = document.createElement("div");
div.innerHTML = "<!--[if lte IE 6]><i></i><![endif]-->";
var isIe6orLower = (div.getElementsByTagName("i").length == 1);

alert("Is IE 6 or lower: " + isIe6orLower);

UPDATE 17 June 2012

Note that IE 10 will not support conditional comments, so this approach will not work in future versions of IE.

Conditional comments:

<!--[if IE 6]>
<script>
var ie6 = true;
</script>
<![endif]-->

I don't think there's a good, generic way of detecting whether or not png transparency is supported. However I all major browsers support it nowadays except for IE6 and very rarely IE5.5. I don't think you'll have to reasonably worry about any other cases.

var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");

if( badBrowser ) {
    // change to "img/horarios2.png"
}

Taken from here.

IE6 can render png transparency via a non-standard css filter, which invokes directX to handle the transparency on IE's behalf.

As for other browsers, only IE was stupid enough to add only partial PNG support. If a browser supports png, you can safely assume it handles transparency as well (except if it's IE).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!