Created
July 3, 2012 21:18
-
-
Save eduardocereto/3043239 to your computer and use it in GitHub Desktop.
Always return top level domain
This file contains 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
/** | |
* First domain where cookies can be set by the browser. | |
*/ | |
function get_top_domain(){ | |
var i,h, | |
weird_cookie='__top_level=cookie', | |
hostname = document.location.hostname.split('.'); | |
for(i=hostname.length-1; i>=0; i--) { | |
h = hostname.slice(i).join('.'); | |
document.cookie = weird_cookie + ';domain=.' + h + ';'; | |
if(document.cookie.indexOf(weird_cookie)>-1){ | |
document.cookie = weird_cookie.split('=')[0] + '=;domain=.' + h + ';expires=Thu, 01 Jan 1970 00:00:01 GMT;'; | |
return h; | |
} | |
} | |
return ''; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fails for
localhost
. In this case you can still set cookies but you need to omit thedomain=
part.