获得网站二级域名的方法?
问题描述:
你知道一种通过类似于 document.location.hostname
的网站获取网站的二级域名的方法吗?
Do you know of a way to get the secondlevel domain of a website via javascript similar to document.location.hostname
?
我想创建一个mailto bookmarklet,它从字符串info @和第二级域生成一个电子邮件地址。 document.location.hostname的问题是www。字首。
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.
答
快速肮脏...
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.