Learn how JavaScript COOKIES work! 🍪

Sdílet
Vložit
  • čas přidán 22. 04. 2022
  • #JavaScript #cookies #tutorial
    // cookie = a small text file stored on your computer
    // used to remember information about the user
    // saved in name=value pairs
    //console.log(navigator.cookieEnabled);
    //document.cookie = "firstName=Spongebob; expires=Sun, 1 January 2030 12:00:00 UTC; path=/";
    //document.cookie = "lastName=Squarepants; expires=Sun, 1 January 2030 12:00:00 UTC; path=/";
    //let allCookies = document.cookie;
    //console.dir(allCookies);
  • Věda a technologie

Komentáře • 41

  • @BroCodez
    @BroCodez  Před 2 lety +41

    const firstText = document.querySelector("#firstText");
    const lastText = document.querySelector("#lastText");
    const submitBtn = document.querySelector("#submitBtn");
    const cookieBtn = document.querySelector("#cookieBtn");
    submitBtn.addEventListener("click", () => {
    setCookie("firstName", firstText.value, 365);
    setCookie("lastName", lastText.value, 365);
    });
    cookieBtn.addEventListener("click", () => {
    firstText.value = getCookie("firstName");
    lastText.value = getCookie("lastName");
    });
    function setCookie(name, value, daysToLive){
    const date = new Date();
    date.setTime(date.getTime() + (daysToLive * 24 * 60 * 60 * 1000));
    let expires = "expires=" + date.toUTCString();
    document.cookie = `${name}=${value}; ${expires}; path=/`
    }
    function deleteCookie(name){
    setCookie(name, null, null);
    }
    function getCookie(name){
    const cDecoded = decodeURIComponent(document.cookie);
    const cArray = cDecoded.split("; ");
    let result = null;

    cArray.forEach(element => {
    if(element.indexOf(name) == 0){
    result = element.substring(name.length + 1)
    }
    })
    return result;
    }



    Document

    first name:

    last name:

    submit
    get cookies

  • @chiculitamihaela8076
    @chiculitamihaela8076 Před rokem +4

    Thank you, great practical examples, I understood cookies 😍

  • @pexeixv
    @pexeixv Před rokem +3

    Was supposed to use the js-cookie library, until I saw this video and decided to write it out myself. Well explained!

  • @ALLINONE-ko6sv
    @ALLINONE-ko6sv Před 2 lety +6

    Completed the JavaScript playlist.. Thanks Bro

  • @mrguy2
    @mrguy2 Před rokem

    Thank you so much this tutorial was very useful and I really learned what I was doing.

  • @kirillzlobin7135
    @kirillzlobin7135 Před 11 měsíci

    Great tutorial. Thank you

  • @daniellinares4960
    @daniellinares4960 Před měsícem

    this is such a life saver, thank you!!

  • @one111won
    @one111won Před rokem

    awesome explanation!

  • @FluoFalI
    @FluoFalI Před 7 měsíci +1

    Finally i can store data within a client your a lifesaver

  • @ripolas
    @ripolas Před rokem +1

    super underrated!!!

  • @MrLoser-ks2xn
    @MrLoser-ks2xn Před rokem

    Thanks!

  • @nischaygadher4093
    @nischaygadher4093 Před měsícem

    Awesome bro 😎👍

  • @RohitCantSing
    @RohitCantSing Před 2 lety

    Can you plz also do fortran tutorials. I need it for my exams but I can't find someone who can explain it well like you

  • @adityashinde7815
    @adityashinde7815 Před 5 měsíci

    thank you so much for this valuable content bro code 🙏🙏🙏🙏🙏🙏🙏

  • @Ken-zh4gu
    @Ken-zh4gu Před 5 měsíci

    Bro thank you for your tutorials and can you please do a course in django python?.

  • @shegyoo
    @shegyoo Před 9 měsíci

    great video

  • @addda1899
    @addda1899 Před 2 lety +8

    Thank you bro not disappointed as always, can you do a video about promises,callbacks,asyncs.. i am really struggling on those

  • @Plapx3
    @Plapx3 Před rokem

    thank bro

  • @coocoo3336
    @coocoo3336 Před rokem +1

    wow greate tutorial

  • @hunin27
    @hunin27 Před 8 měsíci

    Actually, you could use the browser console and go into the application sub-menu, there, cookies, local storage and session storage are displayed in a table!

  • @dmytro_kruhlyi
    @dmytro_kruhlyi Před 10 měsíci

    Thanks for the amazing video! Where is your autocomplete? 😄

  • @TechBro4Life-uk8po
    @TechBro4Life-uk8po Před 2 měsíci

    Very cool my bro code. But i'd want to suggest that you replace == with === in 08:27

  • @mahir04
    @mahir04 Před rokem +3

    Thanks. If one day my startup makes enough money I'll pay you back for this.

  • @fujiaaniya
    @fujiaaniya Před rokem

    Ďakujeme za zdieľanie. Viem, že Morelogin dokáže chrániť súkromie.

  • @lucabinder9829
    @lucabinder9829 Před 2 lety +2

    That was complicated 🥵🥵🥵

  • @TheSavageNoob
    @TheSavageNoob Před 2 lety

    lets go

  • @tayaobilly8577
    @tayaobilly8577 Před 2 lety

    the video title says it all hahaha

  • @Kattoe
    @Kattoe Před rokem

    Is there any way to copy a specified cookie to your clipboard?

  • @zibozhao5789
    @zibozhao5789 Před 2 lety

    when i write document.cookie = "firstName=SpongeBob"; my cookie stays blank idk why /_ \

  • @mdmahmudjamil5802
    @mdmahmudjamil5802 Před 4 měsíci

    sir, How to overcome document.object not defined 😔

  • @rudrarajkar1116
    @rudrarajkar1116 Před 2 lety +5

    is this what consistency is?

  • @crazyMando_me
    @crazyMando_me Před 5 měsíci

    milk and cookies

  • @natnaeltaye
    @natnaeltaye Před 2 měsíci

    i don't understand The , if(element.indexOf(name){
    result = element.substring(name.length + 1);
    }
    is there anyone who can explain to me?

  • @abuzarn7310
    @abuzarn7310 Před rokem +10

    Remove the word "Beginners" from the playList title

  • @Vairoon
    @Vairoon Před 2 lety

    cookies dont work, they stay blank xd

  • @smilekick6025
    @smilekick6025 Před 2 lety +3

    first comment

  • @hamzagohar2048
    @hamzagohar2048 Před 7 dny

    This code is giving errors i copied it from the comment section to see if i made any mistakes but it still gave the same error
    function getCookie(name){
    const cDecoded = decodeURIComponent(document.cookie);
    const cArray = cDecoded.split("; ");
    let result = null;

    cArray.forEach(element => {
    if(element.indexOf(name) == 0){
    result = element.substring(name.length + 1)
    }
    })
    return result;
    }
    error
    TypeError: Cannot read properties of undefined (reading 'length')
    at javaScript.js:25:39
    at Array.forEach ()
    at getCookie (javaScript.js:23:9)
    at javaScript.js:3:1
    (anonymous) @ javaScript.js:25
    getCookie @ javaScript.js:23
    (anonymous) @ javaScript.js:3