Learn how JavaScript COOKIES work! 🍪

Поділитися
Вставка
  • Опубліковано 22 кві 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);
  • Наука та технологія

КОМЕНТАРІ • 40

  • @BroCodez
    @BroCodez  2 роки тому +40

    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

  • @pexeixv
    @pexeixv Рік тому +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 Рік тому +6

    Completed the JavaScript playlist.. Thanks Bro

  • @chiculitamihaela8076
    @chiculitamihaela8076 Рік тому +4

    Thank you, great practical examples, I understood cookies 😍

  • @mrguy2
    @mrguy2 Рік тому

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

  • @daniellinares4960
    @daniellinares4960 21 день тому

    this is such a life saver, thank you!!

  • @kirillzlobin7135
    @kirillzlobin7135 10 місяців тому

    Great tutorial. Thank you

  • @one111won
    @one111won Рік тому

    awesome explanation!

  • @DlguyIsbad
    @DlguyIsbad 6 місяців тому +1

    Finally i can store data within a client your a lifesaver

  • @nischaygadher4093
    @nischaygadher4093 25 днів тому

    Awesome bro 😎👍

  • @ripolas
    @ripolas Рік тому +1

    super underrated!!!

  • @RohitCantSing
    @RohitCantSing 2 роки тому

    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

  • @Ken-zh4gu
    @Ken-zh4gu 4 місяці тому

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

  • @MrLoser-ks2xn
    @MrLoser-ks2xn Рік тому

    Thanks!

  • @dmytro_kruhlyi
    @dmytro_kruhlyi 9 місяців тому

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

  • @shegyoo
    @shegyoo 8 місяців тому

    great video

  • @hunin27
    @hunin27 7 місяців тому

    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!

  • @addda1899
    @addda1899 2 роки тому +8

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

    • @ytgod2094
      @ytgod2094 10 місяців тому +2

      He already did

  • @adityashinde7815
    @adityashinde7815 4 місяці тому

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

  • @coocoo3336
    @coocoo3336 Рік тому +1

    wow greate tutorial

  • @Plapx3
    @Plapx3 Рік тому

    thank bro

  • @TechBro4Life-uk8po
    @TechBro4Life-uk8po Місяць тому

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

  • @mahir04
    @mahir04 Рік тому +3

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

  • @lucabinder9829
    @lucabinder9829 2 роки тому +2

    That was complicated 🥵🥵🥵

  • @Kattoe
    @Kattoe Рік тому

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

  • @fujiaaniya
    @fujiaaniya Рік тому

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

  • @TheSavageNoob
    @TheSavageNoob 2 роки тому

    lets go

  • @mdmahmudjamil5802
    @mdmahmudjamil5802 3 місяці тому

    sir, How to overcome document.object not defined 😔

  • @tayaobilly8577
    @tayaobilly8577 2 роки тому

    the video title says it all hahaha

  • @rudrarajkar1116
    @rudrarajkar1116 2 роки тому +5

    is this what consistency is?

  • @zibozhao5789
    @zibozhao5789 2 роки тому

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

  • @crazyMando_me
    @crazyMando_me 4 місяці тому

    milk and cookies

  • @natnaeltaye
    @natnaeltaye Місяць тому

    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 Рік тому +10

    Remove the word "Beginners" from the playList title

  • @Vairoon
    @Vairoon 2 роки тому

    cookies dont work, they stay blank xd

  • @smilekick6025
    @smilekick6025 2 роки тому +3

    first comment