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

To get cookie using JS (Not USE-API)

Avatar

Avatar
Validate 10
Level 2
v1101
Level 2

Likes

4 likes

Total Posts

150 posts

Correct Reply

0 solutions
Top badges earned
Validate 10
Validate 1
Ignite 1
Give Back 5
Give Back 3
View profile

Avatar
Validate 10
Level 2
v1101
Level 2

Likes

4 likes

Total Posts

150 posts

Correct Reply

0 solutions
Top badges earned
Validate 10
Validate 1
Ignite 1
Give Back 5
Give Back 3
View profile
v1101
Level 2

04-09-2020

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>

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Boost 50
Employee
surabhikatiyar
Employee

Likes

68 likes

Total Posts

46 posts

Correct Reply

18 solutions
Top badges earned
Boost 50
Boost 5
Boost 3
Boost 25
Boost 10
View profile

Avatar
Boost 50
Employee
surabhikatiyar
Employee

Likes

68 likes

Total Posts

46 posts

Correct Reply

18 solutions
Top badges earned
Boost 50
Boost 5
Boost 3
Boost 25
Boost 10
View profile
surabhikatiyar
Employee

06-09-2020

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>

Answers (2)

Answers (2)

Avatar

Avatar
Validate 25
MVP
Singaiah_Chintalapudi
MVP

Likes

131 likes

Total Posts

234 posts

Correct Reply

39 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Ignite 5
Ignite 3
View profile

Avatar
Validate 25
MVP
Singaiah_Chintalapudi
MVP

Likes

131 likes

Total Posts

234 posts

Correct Reply

39 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Ignite 5
Ignite 3
View profile
Singaiah_Chintalapudi
MVP

05-09-2020

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

Avatar

Avatar
Affirm 50
MVP
Vaibhavi
MVP

Likes

216 likes

Total Posts

181 posts

Correct Reply

61 solutions
Top badges earned
Affirm 50
Validate 1
Ignite 1
Give Back 5
Give Back 3
View profile

Avatar
Affirm 50
MVP
Vaibhavi
MVP

Likes

216 likes

Total Posts

181 posts

Correct Reply

61 solutions
Top badges earned
Affirm 50
Validate 1
Ignite 1
Give Back 5
Give Back 3
View profile
Vaibhavi
MVP

04-09-2020

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();
}