DEV Community

Gürkan Biçer
Gürkan Biçer

Posted on

VanillaJS Cookie Yönetimi

Bir cookie.js dosyası oluşturup, içerisine yazıp unutmalık iki fonksiyon.

Bazı zamanlarda front-end tarafında cookie okuyup, yazmak gerekebiliyor.

function getCookie(name) {
    var cookieArr = document.cookie.split(";");
    for (var i = 0; i < cookieArr.length; i++) {
        var cookiePair = cookieArr[i].split("=");

        if (name == cookiePair[0].trim()) {
            return decodeURIComponent(cookiePair[1]);
        }
    }

    return null;
}

function setCookie(name, value, expiryMin) {
    var cookie = name + "=" + encodeURIComponent(value);

    if (typeof expiryMin === "number") {
        cookie += "; max-age=" + (expiryMin * 60);
        document.cookie = cookie;
    }
}
Enter fullscreen mode Exit fullscreen mode

Cookie okumak için;
Cookie yoksa null değer döner.

$ console.log(getCookie('BenimAdim'))
# Kuki
Enter fullscreen mode Exit fullscreen mode

Cookie tanımlamak için;
Son parametrede dakika cinsinden ne kadar süre cookie tutmak istediğimizi belirliyoruz.

$ setCookie('BenimAdim', 'Kuki', 1440)
$ console.log(getCookie('BenimAdim'))
# Kuki
Enter fullscreen mode Exit fullscreen mode

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay