Burfi
Burfi

Reputation: 229

Cookie Detection , Redirect if Non javascript

Tying to write a JavaScript code that will detect if a user has cookies disabled in their browser. If they do, they will be redirected to another page. If they have their cookies enabled it just goes straight through as usual.

i have a code that already works but it still hits the page for a split second before redirection. Is there anyway to make it instant.An example of fast detection is here ( try it with cookies disabled) http://optusnet.com.au You will see it is instant and doesn't load the page request first.

    <script type="text/javascript"> 
/* function to create cookie @param name of the cookie @param value of the cookie @param validity of the cookie */
 function createCookie(name, value, days) 
 { 
    var expires; 
    if (days) 
    { 
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); 
            expires = "; 
            expires=" + date.toGMTString();
    } 
    else 
        expires = ""; 
        document.cookie = name + "=" + value + expires + "; path=/"; 
    }

    function readCookie(name) 
    { 
        var nameEQ = name + "="; 
        var ca = document.cookie.split(';'); 
        for (var i = 0; i < ca.length; i++) 
        {
            var c = ca[i];
            while (c.charAt(0) == ' ') 
                c = c.substring(1, c.length); 
            if (c.indexOf(nameEQ) == 0) 
                return c.substring(nameEQ.length, c.length); 
        }
        return null;
    } 

    function eraseCookie(name)
    {
        createCookie(name, "", -1); 
    }

    /* This function will create a new cookie and reading the same cookie. */
    function areCookiesEnabled() 
    {
        var r = false; 
        createCookie("testing", "Hello", 1); 
        //creating new cookie 
        if (readCookie("testing") != null)
        {
            //reading previously created 
            cookie r = true; 
            eraseCookie("testing");
        }
        return r; //true if cookie enabled.
    } 
    </script> 
    <script> 
        if(!areCookiesEnabled()) 
        { 
            //redirect to page
        }
    </script>

Upvotes: 0

Views: 579

Answers (1)

Ant P
Ant P

Reputation: 25231

You won't be able to make it "instant" with client-side code (i.e. JavaScript) because the page has to be loaded for this code to run. You can achieve this with server-side code, however (like the page you link to does). Here's what you'd have to do, in whatever your server-side language is:

  • On the load of your first page, set a cookie and immediately send a redirect instruction to your main page.
  • On your main page load, check for the cookie. If no cookie exists, cookies are disabled, so redirect to a third (error) page.
  • If the cookie exists, allow the page to load normally.

Alternatively, you can try making it faster by reducing the number of HTTP requests required for your "checking" page (i.e. don't include any styles, etc. on the first page and then redirect to a "proper" page if the test passes) and using more efficient detection methods as per Joe's comment; however, you won't be able to implement an immediate redirect with client-side code.

Upvotes: 1

Related Questions