Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

JavaScript Use of SAME cookies for different functions

Pejamide

New Coder
To my surprise the statement 'document.cookies' cannot be used for different JavaScript functions. so only within the specified JavaScript function. HOW can I use the SAME cookies for some JavaScript cookies?
 
Hello,
The document.cookie property can be used to set and retrieve cookies in JavaScript, but it has some nuances that might be causing confusion.

--
When you call document.cookie, it returns all the cookies in a single string, separated by ;

Example:
console.log(document.cookie); // "username=John; theme=dark"

Limitations:

1. document.cookie does not directly allow structured access to individual cookies; you need to parse the string to access specific cookies.

--

To use the same cookies across multiple JavaScript functions, you can create utility functions for setting, getting, and deleting cookies. This will help you consistently manage cookies throughout your code.

JavaScript:
function setCookie(name, value, days) {
    let expires = "";
    if (days) {
        const date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        expires = "; expires=" + date.toUTCString();
    }
    document.cookie = name + "=" + encodeURIComponent(value) + expires + "; path=/";
}

function getCookie(name) {
    const nameEQ = name + "=";
    const cookies = document.cookie.split('; ');
    for (let i = 0; i < cookies.length; i++) {
        let c = cookies[i];
        if (c.indexOf(nameEQ) === 0) {
            return decodeURIComponent(c.substring(nameEQ.length));
        }
    }
    return null;
}

function deleteCookie(name) {
    document.cookie = name + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
}

Using the Utility function across different scripts:

JavaScript:
setCookie("theme", "dark", 7); 

function applyTheme() {
    const theme = getCookie("theme");
    if (theme) {
        console.log("Theme is:", theme);
        // Apply the theme
    }
}

deleteCookie("theme");

I'm not sure if this answers your questions. But if you need additional help let me know!
 
Yes, I know that procedure. However, I only want to keep EVERY cookie available for every JAVASCRIPT of EVERY webpagina but WITHIN the SAME web domain in stead of within the same webpagina. How can I do so?
 
Yes, I know that procedure. However, I only want to keep EVERY cookie available for every JAVASCRIPT of EVERY webpagina but WITHIN the SAME web domain in stead of within the same webpagina. How can I do so?

Hi,

If I’m understanding what you want - you should be able to use the following code to achieve your goal:

document.cookie = "userSession=abc123; path=/; Secure; SameSite=Strict";

By setting the path=/ when creating cookies, all JavaScript running on the same domain will have access to those cookies, regardless of which page is loaded.
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom