Sep
10

Detect different browsers with Javascript

JavaScript          Trackback

If you are working with JavaScript you were probably in situation that your script is working with some browsers and not working with others. It would help to know which browser is user using. Here is table with some object detection code which can help you to determine that:

Scheme Description
document.getElementById Detects IE5+, Firefox1+, and Opera7+ (modern browsers in general)
window.getComputedStyle Detects Firefox1+ and Opera 8+
Array.every Detects Firefox1.5+ (method detection)
window.Iterator Detects Firefox2+
document.all Detects IE4+
window.attachEvent Detects IE5+
window.createPopup Detects IE5.5+
document.compatMode && document.all Detects IE6+
window.XMLHttpRequest Detects IE7, Firefox1+, and Opera8+
document.documentElement && typeof document.documentElement.style.maxHeight!=”undefined” Detects IE7
window.opera Detects Opera (any version).

You can use this lines of code to detect specific browser:

<script type=”text/javascript”>

if (document.getElementById) alert(“Modern browser detected”)

if (window.getComputedStyle) alert(“Firefox1+ or Opera 8+ detected”)

if (Array.every) alert(“Firefox1.5+ detected”)

if (window.Iterator) alert(“Firefox2+ detected”)

if (document.all) alert(“IE4+ detected”)

if (window.attachEvent) alert(“IE5+ detected”)

if (window.createPopup) alert(“IE5.5+ detected”)

if (document.compatMode && document.all) alert(“IE7, Firefox1+, and Opera8+ detected”)

if (window.XMLHttpRequest) alert(“IE6 detected”)

if (document.documentElement && typeof document.documentElement.style.maxHeight!=”undefined”) alert(“IE7 detected”)

if (window.attachEvent) alert(“Opera (any version) detected”)

</script>


No Comments

Make A Comment

No comments yet.

Comments RSS Feed    TrackBack URL

Leave a comment

top