Quick Javascript tips to help you juggle and handle the known browsers.
Quick browser detection:
Edit: this quick browser detection should be used only to execute IE (or non-standard) Javascript. The way I use this is:
if (window.ActiveXObject) { // IE-only code } else { // standard code }
window.XMLHttpRequest is implemented in IE 7 (but not in IE 6 and below). Thanks to Niko Bellic for the heads up.
IE:
if (window.ActiveXObject)
Mozilla/Safari:
if (window.XMLHttpRequest)
Getting event source element:
IE:
var oSource = event.srcElement;
Moz:
var oSource = event.target;
Cross-browser page redirect:
var sUrl = 'http://adavidchan.com'; setTimeout(function() {window.location.href = sUrl;}, 1);
Does this actually work, I mean I think IE has xmlhttprequest?
Comment by Niko Bellic — July 17, 2007 @ 10:01 am
Yes, IE has implemented XMLHttpRequest with IE 7. I have edited the post to reflect that, and added my suggestion on how to use the quick detection. Thanks for pointing that out!
Comment by David Chan — July 17, 2007 @ 10:44 am
No problem, but do you know of anything that works for detecting all versions of firefox, without wrongly detecting opera as well. I need to detect any and all versions of firefox, but it’s proving a bit difficult.
Comment by Niko Bellic — July 19, 2007 @ 2:23 pm
My script is just a simple object detection; you will need something more comprehensive to get the information you need. A great in-depth browser (including version) detection script can be found at:
http://www.quirksmode.org/js/detect.html
Hope that helps.
Comment by David Chan — July 19, 2007 @ 2:39 pm
Aw man I only wanted to take a moment to say I love to read your Site! SEO Norway.
Comment by SEO Norway — August 30, 2011 @ 7:49 pm