Search code examples
javascripthtmlcookies

Why Doesn't document.cookie work In A Web File?


I Can't Make A Cookie In An Html File...

document.cookie = "Lang:Ar";

When It's Time To Preview:

In Inspect Section > Console :

document.cookie
''

So I Tried To Turn The Cookie Into A Function :

function SetCookie(Key,Val) {
    document.cookie = Key + "=" + Val;
}

But It Didn't Success...

1.Creating The Cookie :

SetCookie("Lang","Ar");
"Lang", "Ar"

2.Previewing It :

document.cookie
''

The Same Thing 😭

But It Worked Only On LiveServer In VScode.


Solution

  • Personnally I'm using this:

    cookieStore.set("cookieName", "cookieValue");
    

    (It only works if you access the document in HTTP or HTTPS)

    You should look at Mamp or Xamp for learning a bit about serving your code locally

    Here is a step by step guide on how to install XAMPP: https://www.youtube.com/watch?v=VCHXCusltqI