...

View Full Version : Detecting browser settings



3D_Dog_Man
04-23-2004, 11:16 AM
Hi Everyone,

Is it possible using javascript, or some other web technolgoy to determine the settings of a users browser. In particular I want to be able to determine whether or not cookies, css and javascript are enabled! I guess using javascript to detect whether or not javascript is enabled is a little backward, but I am stuck so any suggestions will be really appreciated.

Thanks

David

:thumbsup:

jbot
04-23-2004, 11:47 AM
you can't detect CSS, per se, but you can use stylesheets in such a way that those that don't have it are still taken care of.

you use @import for your main stylesheet and <link rel="alternate stylesheet" ...> for those that don't. that way the page degrades silently for them.

to detect for cookies, you say "if (navigator.cookieEnabled) ...". This is for IE4+ and NS6+/Moz.

detecting 4 JS isn't possible, save that you make sure your pages work fairly well without it. alternatively, you can use the <noscript> and meta refresh tags in the head to redirect the user to an alternative page.

:D

Willy Duitt
04-23-2004, 12:40 PM
As Jbot indicated you can not really check if javascript is enabled/disabled. However, you can use javascript to redirect the user to another page. If javascript is enabled, the user will be redirected, if it is disabled, they will remain on the page.

Below is an example of checking for cookies and javascript.


<html>
<head>
<title>Has Cookie</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
body { background : white; color : black; }
</style>

<script type="text/javascript">
function setCookie() {
duit = new Date();
duit.setTime(duit.getTime() + 30*24*60*60*1000);
var expString = "; expires=" + duit.toGMTString();
document.cookie = 'HasCookie' + expString;
}

function chkCookie() {
var dc = document.cookie;
if(!dc.match('HasCookie')) {
alert('You will require cookies enabled to use this site.');
window.location.replace('http://YOUR_NO_COOKIE_REDIRECT_PAGE.html');
}
else {
location.href = location.href;
}
}
window.onload = setCookie
window.setTimeout("chkCookie()", 2000);
</script>
</head>

<body>
<noscript><p>You will require javascript and cookies enabled to use this site.</p></noscript>
HAS COOKIE TEST
</body>
</html>

.....Willy

Kor
04-23-2004, 12:51 PM
As Jbot indicated you can not really check if javascript is enabled/disabled


But you can really do something, for instance to redirect the page if javascript is disabled, which is a check, after all ;)

in head insert

<noscript><meta HTTP-EQUIV="refresh" CONTENT="0;
url=alert.html"></noscript>

and in alert.html put an alert message or whichever

As the meta is <noscript> embeded, the javascript enabled browsers will ignore it, while the javascript disabled will follow the new page

Willy Duitt
04-23-2004, 01:34 PM
But you can really do something, for instance to redirect the page if javascript is disabled, which is a check, after all ;)

I know, that what I said the very next sentence after the one you quoted.


However, you can use javascript to redirect the user to another page. ;)

.....Willy

Willy Duitt
04-23-2004, 01:37 PM
Sorry, I'm a bit slow.
I just understood your redirect. http://www.codingforums.com/images/smilies/redface.gif

.....Willy

Kor
04-23-2004, 06:02 PM
no offence, Willy, I hope... I just wanted to point out that, after all, an alert or a redirect could be almost the same.

brothercake
04-24-2004, 02:59 AM
You can check if CSS is enabled by reference to default property values.

For example - links always have inline display, so if you make a specific link with block-level display, then read its display property out of currentStyle or getComputedStyle, and if the value is not "inline" you can deem CSS to be enabled :thumbsup: It won't work in Safari or early Opera 7 builds, so for them you have to assume styles are always enabled:


//check that nav styles are enabled
function cssIsEnabled()
{
//reference to test link
var linkObj = document.getElementById("testLink");

//link display
var linkDisplay = "inline";

//retrieve the computedStyle display of the link
//this is mozilla and opera 7.2+
if(typeof document.defaultView != "undefined" && typeof document.defaultView.getComputedStyle != "undefined")
{
linkDisplay = document.defaultView.getComputedStyle(linkObj,"").getPropertyValue("display");
}
//retrieve the currentStyle display of the link
//this is internet explorer
else if(typeof linkObj.currentStyle != "undefined")
{
linkDisplay = linkObj.currentStyle.display;
}

//if display is not 'inline' then CSS is deemed to be enabled
//this is also the fallback position if no property is detected
//which is Safari and Opera <7.2
return (linkDisplay != "inline");
};


I trust you want this for something legitimate, not some dodgy redirect or "please upgrade your browser" message ;)

(btw - by "dodgy redirect" I mean any client-side redirect; it's a big accessibility no-no that one, however you implement it)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum