Click to See Complete Forum and Search --> : Javascript To Display Alternate Page


klackenfus
06-08-2006, 12:38 PM
I have created a website that looks great in any standards compliant browser. I'm no artist but it's a basic site that will serve a certain need to me as both a personal website and also, eventually, as a source point from an Open Source database I am creating for a group within the scientific research community. Here's my problem: because I used standards compliant development tools, the main page looks totally wrong in Internet Explorer. I have all the requisite "Use Firefox" stuff on the index page but I know that the majority of my non-computer savvy readers will be using IE and I want it to look good fo them too. I've seen this before but I just can't seem to find it but what would the Javascript be to do the following:

1. Detect the browser type.

2. Based on the browser type, direct the user to either the W3C compliant page or to a page optimized for IE?

Thanks!

ph34r
06-08-2006, 01:58 PM
navigator.AppName and navigator.Version

klackenfus
06-08-2006, 02:40 PM
navigator.AppName and navigator.Version

Thanks for the response. Since HTML is not my strong suit I might need a little more hand holding. Exactly how do I use that?

ph34r
06-08-2006, 03:52 PM
They are built in javascript vairables. Don't forget to use == when checking values, not =.

EnigmaOne
06-08-2006, 04:26 PM
You're actually better off detecting the browser type using server-sides like php, as many folks are disabling java/javascript these days.

bwkaz
06-08-2006, 08:12 PM
No, you're better off not detecting the browser at all.

Let's say you build this huge, elaborate browser sniffing script (either at the server or at the client), that gives you the browser manufacturer (Microsoft, KDE, Mozilla, etc.), the product name (IE, Firefox, Konqueror, etc.), the version, the user's OS, etc., etc. -- all the stuff that will tell you whether the feature you want is supported.

Now, what happens when the next major version of Konqueror is released? Are you sure it's going to act the same way against your page and your scripts? What about IE7? The CSS support is supposed to be better with that (though I've noticed a few problems that still haven't been fixed); are you sure your page still won't work with it? What about the people that set their User-Agent: to the string that IE uses, because several sites that they use require IE? You'll feed them the broken (and out of date?) IE markup, when their browser works just fine with the real site.

Instead of detecting the browser, and then changing the page depending on that, it's much better to just do something that works in all browsers. For instance, if you were detecting the browser in order to decide whether to use window.event or the passed-in event object in a javascript event handler, it's much better to just see whether the passed-in object is defined, and if not, set it to window.event. Likewise, instead of basing the decision of whether to use window.event.srcElement or ev.target on the browser detection, just look to see whether ev.target is defined, and if not, use window.event.srcElement.

That doesn't work too well with the HTML, though, so that's one place where you have to either give up on IE, or figure out a way to make the page work in both browsers as-is.

IMO, the biggest problem with IE6 is that it switches back into quirks mode when you have an otherwise-valid HTML page with a valid DOCTYPE, but that starts with an XML declaration (for instance, <?xml version="1.0" charset="UTF-8" ?>). IE sees the XML declaration and says "oh, that's not a DOCTYPE, I have to be in quirks mode!", but it's wrong, because the DOCTYPE is the next line. But it's too stupid -- the opening angle-bracket for the DOCTYPE has to be the first character of the file for it to work.

The other possible issue might be related to the document's validity -- have you checked it against the W3C's HTML validator?

If it's valid, and the DOCTYPE is first, then the issue is buried somewhere in your CSS or HTML. Perhaps describing the first element (in the HTML source order) that's wrong, along with a link to the page, might shed some light.

klackenfus
06-08-2006, 10:02 PM
No, you're better off not detecting the browser at all.

I totally agree with you. The page that may have a business interest, I might just make a zone transfer off of the main domain and make it plain Jane. Again, I am not a talented web designer. I was just so happy when I saw something decent come up in Firefox and Opera (even Opera is a bit off) that I was hoping for the best with IE. What I got with IE was a mess. We'll see what happens. I'd give out my URL but I need to save the bandwidth. If anyone really wants to see, just send me a private message.