Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Feb 2004
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Detecting browser settings

    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


  • #2
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  • #3
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    Code:
    <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

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Kor
    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

  • #6
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry, I'm a bit slow.
    I just understood your redirect.

    .....Willy

  • #7
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    no offence, Willy, I hope... I just wanted to point out that, after all, an alert or a redirect could be almost the same.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #8
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 It won't work in Safari or early Opera 7 builds, so for them you have to assume styles are always enabled:
    Code:
    //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)
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •