Skip to content

Instantly share code, notes, and snippets.

@aeinbu
Last active November 15, 2015 14:59
Show Gist options
  • Save aeinbu/a22800f57d7c2dbdf884 to your computer and use it in GitHub Desktop.
Save aeinbu/a22800f57d7c2dbdf884 to your computer and use it in GitHub Desktop.
A small utility to read ASP.NET issued cookies from javascript. (Cookies and sub-cookies)
;(function(){
function cookieCutter(cookieName, subCookieName) {
var name = cookieName + "=";
if (subCookieName) {
name += subCookieName + "=";
}
var cookie = document.cookie + ";";
var cookieValueStart = cookie.indexOf(name) + name.length;
var val = cookie.substring(cookieValueStart, cookie.indexOf(";", cookieValueStart));
return val.trim();
};
// Publish the library as an AMD module, CommonJS Module, or to the global window object
if (typeof define === 'function' && define.amd) {
define('cookieCutter', function() {
return cookieCutter;
});
} else if(typeof require === 'function' && typeof exports === 'object' && typeof module === 'object') {
module.exports = cookieCutter;
} else {
window.cookieCutter = cookieCutter;
}
})();
@aeinbu
Copy link
Author

aeinbu commented Aug 8, 2015

How to use

Without a module loader

<script src="cookieCutter.js"></script>
<script>
  var cookieName = '...';
  var subCookieName = '...';
  var res = cookieCutter(cookieName, subCookieName);
</script>

With AMD

require(["cookieCutter"], function(cookieCutter){
  var cookieName = '...';
  var subCookieName = '...';
  var res = cookieCutter(cookieName, subCookieName);
});

With CommonJS

var cookieCutter = require("cookieCutter");
var cookieName = '...';
var subCookieName = '...';
var res = cookieCutter(cookieName, subCookieName);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment