问题
Do you know of a way to get the secondlevel domain of a website via javascript similar to document.location.hostname
?
I want to create a mailto bookmarklet that generates an email address from the string "info@" and the secondlevel domain. the issue with document.location.hostname is the "www." prefix.
回答1:
Quick and dirty...
var domain = location.hostname.split('.').slice(1).join('.');
But that's only going to strip off the first part. If you've got more than one level of server name, that's not going to work. By you should be able to figure it out.
回答2:
Negative number can slice from the end, what gives the second level domain:
var domain=location.hostname.split('.').slice(-2).join('.');
来源:https://stackoverflow.com/questions/10132868/a-way-to-get-the-secondlevel-domain-of-a-website