0

I have a script that set a cookies once a link is clicked and then when when u visited the page again it will check the cookies and alert that you are already here. However my code doesn't seem to work. Can any1 help out??

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
    <title></title>
    <script type="text/javascript">
        function get_cookie("visited"){
            if (document.cookie.indexOf("visited") >= 0) {
                // They've been here before.
                alert("hello again");
            }
    </script>

</head>

<body onload="get_cookie()">

    <script type="text/javascript">
        /*  This function sets the cookie   */
        function iLoveCookies() {
                days = 30; // number of days to keep the cookie
                myDate = new Date();
                myDate.setTime(myDate.getTime() + (days * 24 * 60 * 60 * 1000));
                document.cookie = 'cookieName=visited; expires=' + myDate.toGMTString();
            }
            /*  end of cookie function  */


        
    </script>

    <a href="#" onclick="iLoveCookies()">Set Cookie</a>
</body>
</html>

3
  • You are missing a closing } on your getCookie function. Commented Apr 3, 2015 at 18:19
  • 1
    function get_cookie("visited"){ } is not valid. Commented Apr 3, 2015 at 18:19
  • i took out the } but problem still persit. How exactly i check the cookies that i set in this case. Cause the cookie is being set but not check correctly Commented Apr 3, 2015 at 18:23

1 Answer 1

2

It looks like you basically had typos causing the errors. You might want to use a linter to check your JavaScript or at the least look at your browser's developer console to see what it's telling you.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
    <title></title>
    <script type="text/javascript">
        function get_cookie(){
            if (document.cookie.indexOf("visited") >= 0) {
                // They've been here before.
                alert("hello again");
            }
          }
    </script>
  

</head>

<body onload="get_cookie()">

    <script type="text/javascript">
        /*  This function sets the cookie   */
        function iLoveCookies() {
                days = 30; // number of days to keep the cookie
                myDate = new Date();
                myDate.setTime(myDate.getTime() + (days * 24 * 60 * 60 * 1000));
                document.cookie = 'cookieName=visited; expires=' + myDate.toGMTString();
            }
            /*  end of cookie function  */
    </script>

    <a href="#" onclick="iLoveCookies()">Set Cookie</a>
    <a href="#" onclick="get_cookie()">Get Cookie</a>
</body>
</html>

Sign up to request clarification or add additional context in comments.

1 Comment

Glad to hear it. Just an FYI: there are lots of existing libraries/frameworks that you could use as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.