获取子域名的JavaScript域名 [英] Get the domain name of the subdomain Javascript

查看:106
本文介绍了获取子域名的JavaScript域名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以从可能的子域 sub1.example.com 中获取域名 example.com sub2.example.com sub3.example.com 使用javascript ...?

How i can get the domain name example.com from the set of possible subdomains sub1.example.com sub2.example.com sub3.example.com using javascript ...?

推荐答案

var parts = location.hostname.split('.');
var subdomain = parts.shift();
var upperleveldomain = parts.join('.');

要只获得二级域名,您可以使用

To get only the second-level-domain, you might use

var sndleveldomain = parts.slice(-2).join('.');

这篇关于获取子域名的JavaScript域名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆