Skip to content

Instantly share code, notes, and snippets.

@smhmic
Last active March 3, 2021 21:13
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 smhmic/f775724136ec753bc0e4253bac5a7c4a to your computer and use it in GitHub Desktop.
Save smhmic/f775724136ec753bc0e4253bac5a7c4a to your computer and use it in GitHub Desktop.
Get top-level domain (TLD) of current page
function getTLD(){
var cookieName = '_tmp_detect_tld',
globalNamespacePageloadCache = cookieName;
return window[globalNamespacePageloadCache] || ( window[globalNamespacePageloadCache] = (function(){
var i, domain, parts = location.hostname.split('.'),
UNDEFINED = '{{UNDEFINED}}', cKeyValPair, c;
if( window.navigator && navigator.cookieEnabled === false ){ return UNDEFINED+' cookies disabled'; }
for( i=parts.length-1; i>=0; i-- ){
domain = parts.slice(i).join('.');
cKeyValPair = cookieName+'_'+domain+'=1';
document.cookie = c = cKeyValPair+';domain=.'+domain+';path=/;SameSite=None;Secure;';
if( document.cookie.indexOf(cKeyValPair)>-1 ){
document.cookie = c+'expires=Thu, 01 Jan 1970 00:00:01 GMT;';
// Session cookie might not be deleted in Chrome
//if( document.cookie.indexOf(cKeyValPair)>-1 ){ return UNDEFINED+' failed to remove cookie'; }
return domain;
}
}
return UNDEFINED;
})());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment