SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2011
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Trying to set a cookie in Chrome!

    I have Chrome 16.0.912.63 m. I'm learning js. A simple cookie setting example script works in IE & Firefox but not in Chrome.

    Code HTML4Strict:
    <a href="javascript: setcookie();">Set Cookie</a><br/>
    <a href="javascript: readcookie();">Read Cookie</a>
     
     
    <script type="text/javascript">
     
    	function setcookie(){
    		document.cookie="randomcookietext";
    	}
     
    	function readcookie(){
    		alert(document.cookie);
    	}
     
     
    </script>

    What do I need to do differently in Chrome?

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,688
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Fretman View Post
    What do I need to do differently in Chrome?
    Its not just Chrome. Here is how to properly handle cookies.

    Code javascript:
    function createCookie(name, value, days) {
        var expires = '',
            date = new Date();
        if (days) {
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            expires = '; expires=' + date.toGMTString();
        }
        document.cookie = name + '=' + value + expires + '; path=/';
    }
     
    function readCookie(name) {
        var cookies = document.cookie.split(';'),
            length = cookies.length,
            i,
            cookie,
            nameEQ = name + '=';
        for (i = 0; i < length; i += 1) {
            cookie = cookies[i];
            while (cookie.charAt(0) === ' ') {
                cookie = cookie.substring(1, cookie.length);
            }
            if (cookie.indexOf(nameEQ) === 0) {
                return cookie.substring(nameEQ.length, cookie.length);
            }
        }
        return null;
    }
     
    function eraseCookie(name) {
        createCookie(name, '', -1);
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2011
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    Its not just Chrome. Here is how to properly handle cookies.

    Code javascript:
    function createCookie(name, value, days) {
        var expires = '',
            date = new Date();
        if (days) {
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            expires = '; expires=' + date.toGMTString();
        }
        document.cookie = name + '=' + value + expires + '; path=/';
    }
     
    function readCookie(name) {
        var cookies = document.cookie.split(';'),
            length = cookies.length,
            i,
            cookie,
            nameEQ = name + '=';
        for (i = 0; i < length; i += 1) {
            cookie = cookies[i];
            while (cookie.charAt(0) === ' ') {
                cookie = cookie.substring(1, cookie.length);
            }
            if (cookie.indexOf(nameEQ) === 0) {
                return cookie.substring(nameEQ.length, cookie.length);
            }
        }
        return null;
    }
     
    function eraseCookie(name) {
        createCookie(name, '', -1);
    }
    Thanks for that but it's not as helpful as I'd hoped.

    I began learning javascript a week or two ago. I'm reading Pollock's Javascript - A Beginner's Guide. It is just Chrome since my code does work in IE and Firefox. I'm sure I would eventually have come across the more updated/proper ways of handling cookies but for now I'm looking to find out why my code doesn't work in Chrome. If it's very important that I immediately abandon the way Pollock's book is teaching me then I could do with some reasons why.

    I'm trying to understand the code, I don't want to simply memorize some code that works, I want to understand why it works better or why my code was faulty.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,688
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Fretman View Post
    I'm trying to understand the code, I don't want to simply memorize some code that works, I want to understand why it works or why my code was faulty.
    Google Chrome is much more secure than other web browsers. As a result of that, a local web page has no cookie privileges. It's only if the page is running from a web server that it has permission to use cookies.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2011
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    Google Chrome is much more secure than other web browsers. As a result of that, a local web page has no cookie privileges. It's only if the page is running from a web server that it has permission to use cookies.
    I see, thanks.

  6. #6
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    Google Chrome is much more secure than other web browsers. As a result of that, a local web page has no cookie privileges. It's only if the page is running from a web server that it has permission to use cookies.
    Opera used to be like that too, until we put-on pressure to get it changed.
    Tab-indentation is a crime against humanity.


Bookmarks

Posting Permissions

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