Creating Cookies with Javascript
Mapping Rule - Setting, Managing and Deleting Cookies
First of all, we need to create some functions.
var cookie = { set(name, value, days){ if (!days); var exdate = new Date(); exdate.setDate(exdate.getDate() + days); setExpiry = "; expires=" + exdate.toGMTString(); return macros.put("@SETCOOKIE@","" + name + "=" + value + "; path=/; HttpOnly; Secure" + setExpiry + ";"); }, remove(name){ return macros.put("@SETCOOKIE@","" + name + "=; path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT;"); }, get(name, array){ var cookie = "" for ( i = 0; i < array.length; i++){ cookieArr = array[i].trim().split("="); if (cookieArr[0] == name) cookie = cookieArr[1]; } return cookie; } };
Calling the functions
Next, depending on what you would like you do, paste the following examples into the Infomap that is being called by the browser.
// Return a cookie to the browser. var cookiePayload = "Test" cookie.set("cookieName", cookiePayload, null); // Remove a cookie from the browser. cookie.remove("cookieName"); // Get a cookie from the browser request var cookieArray = context.get(Scope.REQUEST, "urn:ibm:security:asf:request:header", "Cookie").split(";"); var cookie = cookie.get("cookieName", cookieArray);
Template Page - Macro
Lastly, when we created the Infomap, we would have attached a HTML Template page to it.
In this template, paste the below code.
<% var cookie = "" + macro("@SETCOOKIE@"); if(cookie.length > 0){ templateContext.response.setHeader("Set-Cookie", cookie); } %>