Jump to content

Cookie Paths


Jesdisciple
 Share

Recommended Posts

The background for this problem is at this other thread. I made a Cookie class based on http://www.quirksmode.org/js/cookies.html and included it on three pages. My problem is that after the "Go back." link is clicked all other pages redirect to index.html even though I've tried to erase all cookies. The culprit seems to be the cookie which uses the current directory as its path despite the instruction to use the server's document root. I can't figure out why some cookies obey and others don't. (I even tried setting the paths of all cookies to ./ but that's not translated to the current directory.) I've experienced this behavior in Firefox and Opera. Konquerer creates the misbehaving cookie when I click "Go back." but doesn't obey history.back. Any ideas?Cookie.js

function Cookie(name, value, days){    this.name = name;    if(typeof value !== 'undefined')        this.set(value, days);}Cookie.prototype = {    set: function (value, days){        if(days){            var date = new Date();            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));            var expires = "; expires=" + date.toGMTString();        }else            var expires = "";                document.cookie = this.name + '=' + value + expires + '; path=/';    },    get: function (){        var nameEQ = this.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;    },    erase: function (){        this.set('', -1);    }};

index.html

<html>    <head>        <title>Titled Document</title>        <script type="text/javascript" src="Cookie.js"></script>        <script type="text/javascript">            (function (){ // will execute immediately                var cookie = new Cookie('back');                if(cookie.get() !== null)                    cookie.erase();            })();        </script>    </head>    <body>        <a href="test1.html">Test 1</a> <a href="test2.html">Test 2</a>    </body></html>

test1.html and test2.html

<html>    <head>        <title>Titled Document</title>        <script type="text/javascript" src="Cookie.js"></script>        <script type="text/javascript">            (function (){ // will execute immediately                var limit = 10;                var cookie = new Cookie('back');                var back = parseInt(cookie.get());                if(!isNaN(back))                    if(back < limit){                        cookie.set(back + 1);                        history.back();                    }else                        cookie.erase();            })();        </script>    </head>    <body>        <a href="test1.html">Test 1</a> <a href="test2.html">Test 2</a> <a id="back">To go back, click your browser's "Back" button.</a>                <script type="text/javascript">            Element.prototype.on = function (action, response, capturing){                var listener = 'on' + action;                this[listener] = response;                var w3c = 'addEventListener';                var ie = 'attachEvent';                if(typeof this[w3c] === 'function')                    this[w3c](action, response, !!capturing);                else if(typeof this[ie] === 'function')                    this[ie](listener, response);            };            (function (){ // will execute immediately                var back = document.getElementById('back');                back.on('click', function(event){                        document.cookie = 'back=0;';                        history.back();                        return false;                    });                back.innerHTML = 'Go back.';                back.href = '#back';            })();        </script>    </body></html>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...