0

I am new to Cookies and I just want to learn how to create and read a cookie! So I am getting help from http://www.w3schools.com/js/js_cookies.asp But the problem is I just want to create a simple cookie first and then read it! My code is:

<!DOCTYPE html>
<html>
<head>
<script>

function make(){
    document.cookie="username=hellocookie";
}

function read(){
    var x = document.cookie;
    alert(x);
}
</script>
</head>
<body >
    <button type="button" onclick="make()">make</button>
    <button type="button" onclick="read()">read</button>
</body>
</html>

So The Link says:

 document.cookie="username=hellocookie";

will create a cookie for me! so I created like this. The link also says to read all cookies at once I can use:

var x=document.cookie;

So it will return me all cookies!


My problem is that when I use document.cookie to return its value and show it in an alert. My alert shows nothing to me! is it me doing something wrong or something else? I just want to get the cookie I created! (I don't want to use expiry just need to do simple things work first)

Please something help me!

Java Nerd
  • 958
  • 3
  • 19
  • 51
  • just try this site http://www.tutorialspoint.com/javascript/javascript_cookies.htm. here you can also learn how to make and delete or replace cookies – anni May 07 '14 at 13:49
  • 1
    **DON'T** use w3schools! [W3fools information](http://w3fools.com) - Use [Mozilla's js cookies](https://developer.mozilla.org/en-US/docs/Web/API/document.cookie) – Albzi May 07 '14 at 13:49
  • @anni so what is difference between my link and yours lols – Java Nerd May 07 '14 at 13:50
  • 1
    w3schools is a [terrible resource](http://www.w3fools.com/). Try learning from [MDN](https://developer.mozilla.org/en-US/docs/Web/API/document.cookie) instead. With that said, your code works for me. Check to see if you have disabled cookies in your browser. – Emil Vikström May 07 '14 at 13:50
  • when i use w3 schools example it works for me in their try this link! – Java Nerd May 07 '14 at 13:51
  • In future, use [Mozilla's documentation](https://developer.mozilla.org/en-US/docs/Web/API/document.cookie) – Albzi May 07 '14 at 13:52
  • i changed that link. Thanks to all who warns me that w3school is bad – anni May 07 '14 at 13:52
  • so you people dont have an alternative solution to tell me except these useless problems with a link lols – Java Nerd May 07 '14 at 13:57
  • http://stackoverflow.com/a/1022119/3411946 check this good answer – Java Nerd May 07 '14 at 13:58

0 Answers0