Browser Sniffers

T

Trevor L.

I have obtained some browser sniffers from various sources on the net. I
note that Murray makes use of these (or parts of them)
e.g.
<script type="text/javascript">
function print_page(where){
var is_mac=(navigator.platform.indexOf("ac") != -1);
(document.all && is_mac)?
alert("Select \"Print\" from the menu") : where? where.window.print() :
window.print();
}
</script>

I have a script like this (from a good source on the net)
var browser = new Browser()
function Browser()
{ // the now standard browser sniffer class
this.dom = (document.getElementById) ? 1: 0
this.ie4 = (document.all && !this.dom) ? 1: 0
this.ns4 = (document.layers && !this.dom) ? 1: 0
this.ns6 = (this.dom && !document.all) ? 1: 0
this.ie5 = (this.dom && document.all) ? 1: 0
this.ok = (this.dom||this.ie4||this.ns4)
this.platform = navigator.platform
this.is_mac = (this.platform.indexOf("ac") != -1);
}

However, I find that it returns a Javascript error "document" is undefined
referring to the first line in the function

Why is it so?
Can I rewrite this to get rid of the JS error?
--
Cheers,
Trevor L.
Website: http://tandcl.homemail.com.au


I choose Polesoft Lockspam to fight spam, and you?
http://www.polesoft.com/refer.html
 
M

Murray

I never use browser sniffers, except in that particular instance (which is
really a platform sniffer more than a browser sniffer).

Why? They are unreliable, and there are much better ways. What are the
better ways?

1. Build pages with valid code, and
2. Use conditional comments to satisfy IE's eccentricities
 
T

Trevor L.

Hi,

Thanks for your suggestions re browser sniffers, viz. that it is better to
check for features rather than for browsers.

I have looked at a few browser sniffers by googling for them.

Of these, I found that some check for agt.
Others check for features, including these:

document.all
document.getElementByID
document.getElementByTagName
document.layers
document.document.element
document.cookie
document.images
document.forms
document.links
document.addEventListener
window.frames
window.screen

My questions are:

What features is it worth checking for?
That is, which ones differ between browsers?
The reason I ask is that if a feature is common to all, it wouldn't be worth
checking for.

What are the unique/special/eccentric features of IE?
I have used these and I find that they are/may be IE only
images.style.filter
images.filters.BlendTrans.Apply
images.filters.BlendTrans.Play

Are there any others that spring to mind?
What would one need to check for?
(e.g. images.style.filter images.filters )
--
Cheers,
Trevor L.
Website: http://tandcl.homemail.com.au


I choose Polesoft Lockspam to fight spam, and you?
http://www.polesoft.com/refer.html
 
J

Jens Peter Karlsen[FP MVP]

What features you check on depends on what you want to do. Sometimes
there are different ways of doing the same in various browsers so you
check to see what feature is supported in the current browser before
using it to avoid javascript errors.

document.all

IE, Opera (if set to emulate IE)

document.getElementByID

All modern browsers

document.getElementByTagName

All modern browsers

document.layers

Obsolete (NS4 only)

document.document.element

?

document.cookie

All modern browsers (check this before setting a cookie to see if it is
turned of)

document.images
document.forms
document.links
document.addEventListener
window.frames
window.screen

All modern browsers.

Before using any of the above you should explicitly test for them using
an if statement so you can avoid Javascript errors if it is not
supported or possibly disabled (you can disable cookies and images in
most if not all browsers.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top