Posts Tagged ‘javascript’

Detect IE6

February 11, 2008

Since people keep looking for this, here is how you can identify IE 6 (and IE 6 only):

<script type="text/javascript">
//<![CDATA[
var is_ie6 = (
	window.external &&
	typeof window.XMLHttpRequest == "undefined"
);
//]]>
</script>

This is reliable and even Opera is not pretending to be IE6 with this check. This is mostly useful to determine whether the PNG/Alpha Image Loader hack needs to be applied.

UPDATE:

The code provided by Lea Verou below performs up to 50% faster on all browsers except Firefox 2 and Google Chrome (where this test is really slow for some reason; maybe the in operator has not been optimized?)

<script type="text/javascript">
//<![CDATA[
var is_ie6 = ('ActiveXObject' in window && !('XMLHttpRequest' in window));
//]]>
</script>

I cannot wait for the day when we do not have to deal with IE6 anymore. Considering how many intranet applications exist that specifically target this browser, this day may be in a distant future, unfortunately.

Advertisements

JavaScript: img.src = null; // To Load Or Not To Load?

February 3, 2008

Update: I am getting quite a few page views on this post (for a new blog anyway), but I don’t think it is answering what people are looking for. If you could let me know in the comments WHAT it is you were looking for, maybe I can answer it?

Don’t set image.src to null, otherwise IE tries to load the null image.

I was working on a dynamic image popup that shows a spinning icon until the actual image is loaded and among the few lines of JavaScript code I had this (for reasons not important to this discussion):

// loadImg was created earlier with document.createElement('IMG');
loadImg.src = null;

I did not think about side effects when I wrote this, but expected nothing to happen from this assignment. Turns out Internet Explorer is trying to download an image with the relative path null from the server. I found this during testing when validating expectations of connections made to the server. I guess one could argue that IE is behaving correctly and all the other browsers, which are not requesting anything, are behaving incorrectly. I am not sure. To me null means nothing, which implies that nothing gets loaded.

I changed the logic a bit and removed the offending lines. I should start tracking the amount of time I spend on fixing Internet Explorer-related issues and bill Microsoft, because it is starting to get expensive for me.