-1

How can I add a cookie which expires in 2 months to 'test@gmail.com' from the following code

<div id="home">
    <div class="name" my-data="123">Email: test@gmail.com</div> 
</div>

I think the code for the actual time should be as follows but I do not know how to add the value to it:

function myemail () {
    var expiryDate = new Date();
  expiryDate.setMonth(expiryDate.getMonth() + 2);
    document.cookie = cookieName + '=y; expires=' + expiryDate.toGMTString
  • Does this answer your question? [javascript set cookie with expire time](https://stackoverflow.com/questions/13154552/javascript-set-cookie-with-expire-time) – Kris Oct 19 '21 at 12:45
  • no because I still do not understand how to add the cookie to html part of the code 'test@gmail.com' – user16394386 Oct 19 '21 at 12:52
  • What do you mean by 'html part' ? Cookies can be managed by JS – Kris Oct 19 '21 at 12:56
  • 1
    This is what I need to do : Write a JavaScript code snippet that sets a cookie called 'myemail' with the value 'test@gmail.com' with a 2 months expiration. – user16394386 Oct 19 '21 at 13:02

2 Answers2

1

Assuming you have privileges to add an ID to your div, the following code works fine

<body>

    <div id="home">
        <div class="name" my-data="123" id="email-div">Email: test@gmail.com</div>
        <button onclick="myemail()">Set it</button>
    </div>

    <script>
        function myemail() {
            var expiryDate = new Date();
            var email = document.getElementById("email-div").textContent.split("Email:")[1]
            expiryDate.setMonth(expiryDate.getMonth() + 2);
            document.cookie = 'emailCookie=' + email + ';expires=' + expiryDate.toGMTString() + ';path=/';
        }
    </script>

</body

Well I have used a button to trigger the action. You can do the same in any event.

Kris
  • 8,680
  • 4
  • 39
  • 67
  • Can it be done without adding a new id? – user16394386 Oct 19 '21 at 13:16
  • 1
    You can use document.getElementsByClassName("name")[0] instead of ID, but only if you are sure, the class name is not used else where. Or XPath, if you are sure about the tag position, and it will not change – Kris Oct 19 '21 at 13:18
  • I hope you are not trying for XSS :D – Kris Oct 19 '21 at 13:19
0

toGMTString is a function, you need to call. Replace expiryDate.toGMTString to expiryDate.toGMTString()

const Cookie = {
get: function (name) {
    const nameEq = name + "=";
    const ca = document.cookie.split(';');
    for (let i = 0; i < ca.length; i++) {
        let 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;
},

set: function (name, value, hours) {
    const expires = "";
    if (hours) {
        let date = new Date();
        date.setTime(date.getTime() + (hours * 60 * 60 * 1000));
        const expires = "; expires=" + date.toGMTString();
    }

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

You can change hours to whatever and of course this one date.setTime(date.getTime() + (hours * 60 * 60 * 1000)); to the corresponding formula

Usage`

Set: Cookie.set('myemail', 'test@gmail.com', 1460) // 1460 is 2 months in hours

Get: Cookie.get('myemail')

const email = document.getElementsByClassName('name')[0].textContent.split(' ')[1]; Cookie.set('myemail', email, 1460);

Tatul Mkrtchyan
  • 361
  • 4
  • 8