a way to get the secondlevel domain of a website?

2019-08-26 19:07发布

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.

2条回答
爷的心禁止访问
2楼-- · 2019-08-26 19:52

Negative number can slice from the end, what gives the second level domain:

var domain=location.hostname.split('.').slice(-2).join('.');
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-08-26 20:03

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.

查看更多
登录 后发表回答