a way to get the secondlevel domain of a website?

你说的曾经没有我的故事 提交于 2019-12-12 03:59:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!