0

I'm reading 'beginners Javascript'and I'm in the chapter on cookies. They provide an example on how to store and display cookies, but for some reason my alert(document.cookie) returns nothing. Can anyone see why?

<!DOCTYPE html>
<html lang="en">
<head>
<title>Chapter 13: Example 1</title>
</head>
<body>

<script>

function setCookie(name, value, path, expires) {
    value = escape(value);

    if (!expires) {
        var now = new Date();
        now.setMonth(now.getMonth() + 6);
        expires = now.toUTCString();
    }

    if (path) {
        path = ";Path=" + path;
    }

    document.cookie = name + "=" + value + ";expires=" + expires + path;
}

setCookie("Name", "Bob");
setCookie("Age", "101");
setCookie("FirstVisit", "10 May 2007");
alert(document.cookie);
</script>
</body>
</html>
Tijl Declerck
  • 105
  • 1
  • 11

1 Answers1

1

You need to serve the file to make the cookie work.

Like, run it on node and pull it up one localhost:8080/file or something like that.

hamza765
  • 106
  • 14