Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

To get cookie using JS (Not USE-API)

v1101
Level 5
Level 5

Hello Community - Could you give some idea about to fetch the cookie in the component using JavaScript/Jquery but not USE-API. and I wanted to pass the cookie value to WCMUsePojo using data-sly-use.

 

<div data-sly-use.userInfo="${'com.xyz.aem.core.models.UserDetails' @ childNode='test', cookieValue={Need to Pass the cookie value}"  data-sly-unwrap>

1 Accepted Solution
surabhikatiyar
Correct answer by
Employee
Employee

The cookie value cannot be sent through Javascript to the WCMPojo, because the javascript section loads after the HTML/JSP has been loaded. So, even if you fetch the cookie value using Javascript it will never reach the WCMPojo class until you reload via some dynamic template binding.

 

The other options are:-

1. Directly fetch the cookie in WCMPojo class using the request object

 

Cookie[] cookies = request.getCookies();
for (Cookie cookie: cookies) {
    String value = cookie.getValue();
}
 
2. Use the  USE-API
 

-------- script.js -------------

// Server-side JavaScript to get Coookie

use(function () {

var cookie = request.getCookie("wcmmode");

var cookieFound=false;

if(cookie!=null && cookie.value=="edit"){

cookieFound = true;

}

return {

cookieVal: cookie.value,

cookieFound:cookieFound

};

});

-------- Sightly code --------------

<sly data-sly-use.cObj="script.js" data-sly-test="${cObj.cookieFound }">

${cObj.cookieVal}

</sly>

View solution in original post

4 Replies
Vaibhavi
Community Advisor
Community Advisor

Hi @v1101 , 

 

You cannot pass the value from client to server side. 

If you need to set any html attribute value through js , yes it is possible using js functions but here you are trying to pass the value set from client to server which not possible. 

You can use any one of the option below to fetch the cookie. 

1.Using USE-API,fetch the value in js using below method 

request.getCookie("cookiename")

2. Instead of passing the cookie from slightly, fetch the cookie directly in wcmpojo class(java) . 

Cookie[] cookies = request.getCookies();
for (Cookie aCookie : cookies) {
    String name = aCookie.getName();
    String value = aCookie.getValue();
}
Singaiah_Chintalapudi
Community Advisor
Community Advisor

It is much easier to use HTML attributes of this div tag and use JS/jQuery to inject the cookie value into this cookieValue attribute.

 

What's the requirement to fetch this on sightly?

 

Thanks,

Singaiah

v1101
Level 5
Level 5
@Singaiah_Chintalapudi - We need to get the cookie value using JavaScript and then pass the cookie value in data-sly-use as a parameter, so that the value will be retrieved in WCMUsePojo to perform some manipulation based on the value.
surabhikatiyar
Correct answer by
Employee
Employee

The cookie value cannot be sent through Javascript to the WCMPojo, because the javascript section loads after the HTML/JSP has been loaded. So, even if you fetch the cookie value using Javascript it will never reach the WCMPojo class until you reload via some dynamic template binding.

 

The other options are:-

1. Directly fetch the cookie in WCMPojo class using the request object

 

Cookie[] cookies = request.getCookies();
for (Cookie cookie: cookies) {
    String value = cookie.getValue();
}
 
2. Use the  USE-API
 

-------- script.js -------------

// Server-side JavaScript to get Coookie

use(function () {

var cookie = request.getCookie("wcmmode");

var cookieFound=false;

if(cookie!=null && cookie.value=="edit"){

cookieFound = true;

}

return {

cookieVal: cookie.value,

cookieFound:cookieFound

};

});

-------- Sightly code --------------

<sly data-sly-use.cObj="script.js" data-sly-test="${cObj.cookieFound }">

${cObj.cookieVal}

</sly>

View solution in original post