Expand my Community achievements bar.

Writing a cookie before redirect to an external site that is not AEM

Avatar

Level 8

Hi

I need to write a cookie before redirecting to an external site that is not AEM. The redirect site is a normal link. What is the recommended approach to do that?

2 Replies

Avatar

Community Advisor

Hi @anasustic Use can use the below javascript section for adding the cookie.

 

$(function() {
   $(document).on( "click", "a" function() {
   var currentElement = $(this);
   var url = $(this).attr("href");
   if(!link_is_internal(url)) {
     document.cookie = 'cookie-value'; // setting a new value
   }
});

function link_is_internal(url) {
 return (url.indexOf(window.location.host) !== -1 || url.startsWith("/"));
}
});

 

 link_is_internal method over here is handing both the absolute url and the internal link over here, before returning the result.

Hope this helps!

Thanks