Javascript – Incorrectly defined function

This the javascript function:

//set cookies
function setCookie (cname, cvalue) {
    var now = new Date();
    var time = now.getTime();
    var expireTime = time + 1000 * 3600 * 24 * 365;

    now.setTime(expireTime);

    document.cookie = cname + '=' + cvalue + ';expires=' + now.toGMTString() + ';path=/';
}

The error occurs when I call the function from another javascript file:

document.addEventListener("DOMContentLoaded", function () {
    setCookie("showAdminUser", false);    
});

This is the error in Crhome console:

Uncaught TypeError: setCookie is not a function
    at HTMLDocument.<anonymous>

I have thought, that the function was still missing a "return", but it keeps giving me the error in console:

//set cookies
function setCookie (cname, cvalue) {
    var now = new Date();
    var time = now.getTime();
    var expireTime = time + 1000 * 3600 * 24 * 365;

    now.setTime(expireTime);

    document.cookie = cname + '=' + cvalue + ';expires=' + now.toGMTString() + ';path=/';

    return document.cookie;
}

What could be the problem? thanks.



Read more here: https://stackoverflow.com/questions/68480773/javascript-incorrectly-defined-function

Content Attribution

This content was originally published by Eladerezador at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: