Monday, July 6, 2009

Obtaining the Domain Name Through Javascript

In JavaScript, you can retrieve the domain name of the current web page with the domain property of the document object. The document.domain property contains a string which contains the domain, including the "www" prefix if it's present in the URL, and the domain extension, such as "com", if a domain extension is present in the URL. The extension won't be present in the URL if the URL specifies a local server. The prefix, domain, and extension are separated by period characters.

The method below, writeDomain(), is used to write the current domain to this page. The current domain is: www.codehouse.com
function writeDomain()
{
var myDomain = document.domain;

document.write(myDomain);
}

Unfortunately, there is no way of obtaining the IP address with JavaScript.

Thanks,
Bala