Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Transfer custom cookies in a community between Lightning and Visualforce with Locker Service enabled.
* Transfer cookies between Lightning (with Locker Service) and Visualforce. Cookies
* must be set as URL parameters and the "nextURL" parameter must be supplied.
* Cookies will be set for apex using the apex__ prefix and will be set for
* Lightning (with Locker Service) using the LSKey[<namespace>] prefix. Note that
* all cookies will be set as session cookies. If cookies should be given an
* expiration, the next page you navigate to should adjust the cookie expiration.
(function() {
* The namespace for your application. If this is not in a package, set the
* namespace to "c". Note that this is case sensitive.
* @type {String}
var namespace = '<YOUR_NAMESPACE>';
* Create a cookie for the given name, value, and expiration in days.
* @param {String} name The name of the cookie to set.
* @param {String} value The value of the cookie to set.
* @param {Integer} days The number of days the cookie is good for. If not
* supplied a session cookie will be set.
function createCookie(name, value, days) {
var expires;
if(days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
else {
expires = "";
// Note: the name would usually be escaped when setting a cookie but
// Locker Service uses braces in the cookie name.
document.cookie = name + "=" + escape(value) + expires + "; path=/";
* Get the query parameters for the current url as an object.
* @return {Object} An object with a key for each query parameter.
function getQueryParams() {
var qs ='+').join(' ');
var params = {};
var tokens;
var re = /[?&]?([^=]+)=([^&]*)/g;
while((tokens = re.exec(qs)) !== null) {
params[decodeURIComponent(tokens[1])] = decodeURIComponent(tokens[2]);
return params;
* Set cookies for both Apex and Visualforce for the given name and value.
* @param {String} name The cookie name.
* @param {String} value The cookie value.
function setCookies(name, value) {
createCookie(getApexCookieName(name), value, null);
createCookie(getLightningCookieName(name), value, null);
* Get the apex specific cookie name.
* @param {String} name The generic cookie name.
* @return {String} The name of the cookie as handled in Apex.
function getApexCookieName(name) {
return 'apex__' + name;
* Get the Lightning (Locker Service) specific cookie name.
* @param {String} name The generic cookie name.
* @return {String} The name of the cookie as handled in Locker Service.
function getLightningCookieName(name) {
return 'LSKey[' + namespace + ']' + name;
* Transfer all url parameters to Apex and Lightning (with Locker Service)
* cookies. This will update window.location to nextURL after all cookies have
* been set.
* @throws An exception if the "nextURL" query parameter is not present on
* the page.
function doTransfer() {
var params = getQueryParams();
var nextURL = params.nextURL;
delete params.nextURL;
if(!nextURL) {
throw 'No next page found. Please ensure the nextURL parameter is set in the URL. Note that this is case sensitive.';
for(var key in params) {
if(params.hasOwnProperty(key)) {
setCookies(key, params[key]);
window.location = nextURL;
// Run the transfer which will redirect the user after updating all
// cookie values.
<apex:page showHeader="false" sidebar="false">
Replace <your_resource> with the name of the static resource with
your application files in it. This assumes that you are using a zipped static
resource with your javascript files in a 'js/' folder.
<apex:includeScript value="{!URLFOR($Resource.<your_resource>, '/js/transfer-session.js')}" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment