Forum Moderators: open

Message Too Old, No Replies

browser detection - object method

         

sssweb

4:09 pm on Mar 14, 2006 (gmt 0)

10+ Year Member



I need to output different code based on the user's browser, and have read that the best method for doing this is the 'object' method. I put together the following test to try this out, but I'm not getting the right ID's:

<script language="JavaScript" type="text/javascript">
var browser = (document.all)? "IE" :
(document.layers)? "Netscape" : "other"
alert(browser);
</script>

This gives me "other" for Netscape, and "IE" for Opera, even when it's set to ID itself as Opera.

What's the best way to test for this? (I only need to know IE, Netscape, and 'other'.)

sssweb

4:11 pm on Mar 14, 2006 (gmt 0)

10+ Year Member



Hmmm...now that I'm re-reading that, I realize I'm using the 'document' method. What would be code for the 'object' method?

Fotiman

4:35 pm on Mar 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The practice of browser sniffing is not recommended. Plus if someone has JavaScript disabled, your sniffing is not going to work. Just my 2 cents.

DrDoc

5:06 pm on Mar 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Instead of sniffing for a browser ... something like this should work:

if(document.getElementById && document.createElement) {
//browser supports reasonable DOM/DHTML functions
}

pinterface

5:12 pm on Mar 14, 2006 (gmt 0)

10+ Year Member



Object method detection means you test for the objects you want to use, not specific browsers. Thus, if you want to use getElementById() or getElementsByTagName(), you check to see if that method exists and, iff it does, use it.

The entire point of object detection is that you don't care which browser is being used, but instead what that browser is capable of. If you're trying to use object detection to determine if a browser is IE, Netscape, or something else, you're doing it wrong.

Ask Google about [javascript object detection]. quirksmode [quirksmode.org] is a pretty good resource, and I'm sure you'll find others.

[edited by: DrDoc at 6:03 pm (utc) on Mar. 14, 2006]
[edit reason] linked to resource [/edit]

DrDoc

6:05 pm on Mar 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Then again, if you do want to find the browser, you need to query the UserAgent. Just keep in mind that UAs can easily be spoofed. Also, you should not use a UA sniffer to determine actual functionality. Such is best handled by querying the browser for support for the specific functionality you wish to employ.

sssweb

7:44 pm on Mar 14, 2006 (gmt 0)

10+ Year Member



Thanks for all that. Below is the script section that requires certain funcionality. I see getElementById, but I'm not sure about testing for the next part, or if I need to:

if (document.getElementById){
var imgobj=document.getElementById(loadarea)
if (imgobj.filters && window.createPopup){
imgobj.style.filter=filterstring
imgobj.filters[0].Apply()
}

The script says it works with IE5+, NS6+, and Opera 7+. From what I've read about function support for those three, 'getElementById' is the one. Does 'imgobj.filters && window.createPopup' matter?

P.S. Doc - I don't find 'document.createElement' in the script; does that mean it's not used, or are there other expressions using that function? The only things resembling 'create' are the popup thing noted above, and this line:

var myimage=new Image()

DrDoc

4:23 pm on Mar 15, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, your script doesn't use it ... but you should make a habit of putting both in your if statement anyway, just to ensure that it doesn't malfunction in certain older browsers. For this particular case, you're fine.

sssweb

6:01 pm on Mar 15, 2006 (gmt 0)

10+ Year Member



okay, thanks.