I've done further testing and updated the code to manage Netscape
quirks:
---------------
// object detection for jQuery-supported browsers, version 2
var isIE = (document.compatMode && document.all); // IE 6+
var isFF = (Array.every || window.Iterator); // FF 1.5+
if (!isIE && !isFF && document.getElementByID) {
var detect = navigator.userAgent.toLowerCase(); // forced to do a
browser check
var isN6 = ( detect.indexOf("netscape6") > 0 );
var isOther = (typeof document.documentElement.style.maxHeight !=
"undefined") && (!document.all) && !isN6; // Safari, Opera...
}
var jQ = false;
if (!isIE && !isFF && !isOther) {
window.location.replace("index2.html");
} else {
jQ = true;
document.write('<script language="javascript" src="jquery.js"><\/
script>');
document.write('<script language="javascript" src="extras.js"><\/
script>');
}
---------------
This is also updated at http://www.tordevries.com/browser-check/. I
tested this on a Mac OS X and a Windows XP system, with the following
success:
Stays on the jQuery page and works fine:
- IE 6 on XP
- Firefox 2 on XP
- Firefox 2 on OS X
- Firefox 1.5 on OS X
- Safari 2 on OS X
- Netscape 7 on OS X
Forwards to non-jQuery page, as it should:
- IE 5.2 on OS X
- IE 5.1 on OS 9
- IE 4.5 on OS 9
- Netscape 6 on OS X
- Netscape 4.79 on OS 9
- Netscape 3 on OS 9 (just for fun)
In other words, when the browser really does support jQuery, the site
stays with the jQuery-enabled page; otherwise it forwards the user to
a non-jQuery page. I wish I could test older browsers on the Windows
system, but I can't have multiple browser versions installed as I can
on the Mac (all those OS X and OS 9 browsers ran from the same
computer).
Most people won't need this kind of checking -- you are going to use
jQuery and assume the browser will support it. But in some cases I
don't have that luxury, and need to offer something usable (without
script errors) for older browsers.
Of course, I'm not sure Netscape 7 really ought to work with jQuery...
it does the animations properly on my sample homepage (linked above),
but I've heard Ajax won't work right. At least Netscape 9 uses the
same engine as Firefox.
-tdv
On Aug 21, 2:27 pm, Dekortage <[EMAIL PROTECTED]> wrote:
> Hey all...
>
> I've been experimenting with jQuery a lot, and really like the
> functionality it provides. However, there are times when I'd like to
> use jQuery, but need to provide an alternative site for people with
> older browsers (particularly visitors from rural locations in
> developing countries, who often have much older and slower
> technology).
>
> So, I need to detect the browser and make sure it can support jQuery.
> Here is the basic code I've developed, which I've tested in a handful
> of browsers.
>
> // object detection for jQuery-supported browsers
> var isIE = (document.compatMode && document.all); // IE 6+
> var isFF = (Array.every || window.Iterator); // FF 1.5+
> var isOther = (typeof document.documentElement.style.maxHeight !=
> "undefined") && (!document.all); // Safari, Opera...
> var jQ = false;
>
> // act on browser type
> if (!isIE && !isFF && !isOther) {
> window.location.replace("index2.html");} else {
>
> jQ = true;
> document.write('<script language="javascript" src="jquery.js"><\/
> script>');
> document.write('<script language="javascript" src="extras.js"><\/
> script>');
>
> }
>
> You can test this athttp://www.tordevries.com/browser-check/. The
> code uses object detection to identify browsers, though I'm not sure
> this is the most optimal way of doing it. If none of the detection
> booleans is true, the user is booted to a backup page (index2.html);
> otherwise, the script dynamically loads jQuery and an external
> JavaScript file. (I use a meta-refresh tag in the <noscript> tag to
> forward people who have disabled JavaScript.)
>
> Oddly, if you use $(document).ready in the dynamically-loaded
> extras.js file, Safari won't load it. I can't figure out why. Other
> browsers are fine. So I fall back to an onLoad trigger in the body
> tag, referencing a function in the extras.js file. Unfortunately,
> older browsers (like IE 5 on the Mac) may trigger onLoad before they
> get forwarded to the secondary page, thus generating an error because
> the function hasn't been loaded. Thus, the body tag actually looks
> like this:
>
> <body onLoad="if (jQ) init();">
>
> If jQ is true, then onLoad can safely fire the init() function.
> Otherwise it's ignored.
>
> So... thoughts? comments? Does the test page work for you? Is there
> a better way to do this? Thanks in advance.
>
> -tdv