Created
November 27, 2016 20:24
-
-
Save thyngster/0bcaae92056c86a9cdb4cc5473c07eb5 to your computer and use it in GitHub Desktop.
Grab top domain name based on cookie write testing
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
var getRootDomain = function(domain=document.location.host){ | |
var rootDomain = null; | |
if (domain.substring(0,4)=="www.") { | |
domain=domain.substring(4,domain.length); | |
} | |
var domParts = domain.split('.'); | |
for(var i=1;i<=domParts.length;i++) | |
{ | |
document.cookie="testcookie=1; path=/; domain="+domParts.slice(i*-1).join('.'); | |
if(document.cookie.indexOf("testcookie") != -1){ | |
var rootDomain = domParts.slice(i*-1).join('.'); | |
document.cookie="testcookie=1; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/; domain="+domParts.slice(i*-1).join('.'); | |
break; | |
} | |
} | |
return rootDomain; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment