Last active
March 3, 2021 21:13
-
-
Save smhmic/f775724136ec753bc0e4253bac5a7c4a to your computer and use it in GitHub Desktop.
Get top-level domain (TLD) of current page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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