Obtains the root domain of the current domain name.
1 /// <summary> 2 /// obtain the root domain of the current domain name 3 /// </summary> 4 /// <param name = "url"> domain name address </param> 5 // <returns> </returns> 6 public static string RootDomain (string url) 7 {8 if (! Url. StartsWith ("http ://")&&! Url. startsWith ("https: //") 9 {10 url = "http: //" + url; 11} 12 13 var uri = new Uri (url ); 14 string rootDomain; 15 switch (uri. hostNameType) 16 {17 case UriHostNameType. dns: 18 {19 if (uri. isLoopback) 20 {21 rootDomain = uri. host; 22} 23 else24 {25 string host = uri. host; 26 var hosts = host. split ('. '); 27 rootDomain = hosts. length = 2? Host: string. format ("{0 }. {1} ", hosts [1], hosts [2]); 28} 29} 30 break; 31 default: 32 rootDomain = uri. host; 33 break; 34} 35 return rootDomain; 36}
When searching for such problems on the internet, regular expressions are generally used, and several domain name suffixes are fixed, for example. cn ,. com ,. net, so I wrote this method according to my work needs.
For reference.
How to obtain the root domain name in java
String path = request. getContextPath ();
String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";
How to obtain the root domain name of the current page using Firefox js script
Hello! I'm glad to answer your questions!
Try this:
<Script language = "javascript">
// Obtain the Domain Name
Host = window. location. host;
Host2 = document. domain;
// Obtain the complete address of the page
Url = window. location. href;
Document. write ("<br> host =" + host)
Document. write ("<br> host2 =" + host2)
Document. write ("<br> url =" + url)
</Script>
You can learn more in the Firefox community. I hope my answers will be helpful to you. If you have any questions, please continue to consult on this platform.