Skip to content

Instantly share code, notes, and snippets.

@cvan
Created September 3, 2019 03:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cvan/c95a52edc2a35b84a987249ec2a60014 to your computer and use it in GitHub Desktop.
Save cvan/c95a52edc2a35b84a987249ec2a60014 to your computer and use it in GitHub Desktop.
get root domain from URL (JavaScript) for setting `Domain` in `Cookie` header
const getOrigin = str => str.replace(/.*\/\//, '').replace(/:.+/, '').replace(/\/.+/, '');
const getOriginRoot = (str) => {
const originFull = getOrigin(str);
if (!originFull.includes('.')) {
return str;
}
const originFullChunks = originFull.split('.');
const originRoot = originFullChunks.slice(originFullChunks.length - 2, originFullChunks.length).join('.');
return originRoot;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment