Implementing Cookie Settings in SAP UI5


With the constant evolution of the web and the various platforms that power it, ensuring a seamless user experience is crucial. In SAP UI5, a key component that aids in enhancing this experience is the use of cookies. Through this article, we’ll delve into the realm of cookies in SAP UI5, why they’re essential, and how to set them up for optimal use.

What is a Cookie and why is it required in SAP UI5?

A cookie is a small piece of data sent from a website and stored in a user’s web browser. They remember stateful information for the stateless HTTP protocol. In SAP UI5, cookies play an essential role in retaining user preferences, maintaining sessions, or even tracking user interactions, ensuring users don’t have to re-enter preferences every time they revisit an application.

Client-side Cookies vs. server-side Cookies

In the realm of web development, cookies can generally be categorized into two types:

  • Client-side Cookies: These are stored on the user’s browser and can be read using JavaScript when the user revisits the site. They are primarily used for local preferences, theme settings, or even short-term tracking.
  • Server-side Cookies: These are processed on the web server and can be read on subsequent HTTP requests made to the server for the same domain. They are mainly utilized for sessions, user authentication, and more persistent tracking.

In SAP UI5, depending on your requirements, you might lean towards one type of cookie over the other, or even use a combination of both.

Set cookie and get a cookie with JavaScript in SAP UI5

Using JavaScript in SAP UI5 to set and retrieve cookies is straightforward.

Setting a Cookie:

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 + "=" + (value || "") + expires + "; path=/";


setCookie("userPreference", "darkTheme", 7);

Getting a Cookie:

function getCookie(name) {
    const nameEQ = name + "=";
    const cookiesArray = document.cookie.split(';');
    for(let i=0; i < cookiesArray.length; i++) {
        let cookie = cookiesArray[i];
        while (cookie.charAt(0) == ' ') cookie = cookie.substring(1, cookie.length);
        if (cookie.indexOf(nameEQ) == 0) return cookie.substring(nameEQ.length, cookie.length);
    return null;


const userPreference = getCookie("userPreference");


Adding Cookies to a Request

When making AJAX calls in SAP UI5, you might need to send cookies along with your request. Here’s a simple way to do it:

Using the jQuery ajax method:

    url: "",
    type: "GET",
    xhrFields: {
        withCredentials: true
    success: function(data) {
        // Handle data here

The withCredentials: true ensures that cookies are sent with the request. Remember that the server also needs to accept credentials from the origin by setting the appropriate CORS headers.


  • Barry Allen

    A Full Stack Developer with 10+ years of experience in different domain including SAP, Blockchain, AI and Web Development.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.