There are many
JavaScript resources to parse an URL in JavaScript RegEx. And parsing the domain main is easy since there's a lot of trick. This is useful f...
detail at
JavaScriptBank.com - 2.000+ free JavaScript codes
How to setup
Step 1: Use JavaScript code below to setup the script
JavaScript
Code:
<script type="text/javascript">
// Copyright: ivanceras - http://www.ivanceras.com/
/*
This script downloaded from www.JavaScriptBank.com
Come to view and download over 2000+ free javascript at www.JavaScriptBank.com
*/
/**
*
* USAGE:
* UriParser.parseRootDomain("http://user:password@www.truste.com.ca:80/pathname?querystring&key=value#fragment"); //= truste.com.ca
* UriParser.parseRootDomain("http://google.com.ph/pathname?querystring&key=value#fragment"); //= google.com.ph
* UriParser.parseRootDomain("http://www.google.com/pathname?querystring&key=value#fragment"); //= google.com
*
*/
var UriParser = {
//parse the root domain, exclude the sub domain
parseRootDomain : function(url) {
var matches = url.match(/^((\w+):\/\/)?((\w+):?(\w+)?@)?([^\/\?:]+):?(\d+)?(\/?[^\?#]+)?\??([^#]+)?#?(\w*)/);
//my additional code
var theDomain = matches[6];
if(UriParser.isIp(theDomain)){//if it is an ip address return it as domain
return theDomain;
}
var dots = theDomain.split('.');
var n = dots.length;
if(n < 3){//google.com, 2 root words concatenate, 1 word as well i.e. localhost
return dots.join(".");
}
else{//should be greater than or equal to 3 dot split ie. adsense.google.com
var last = dots[n-1];
var second2l = dots[n-2];
var third2l = dots[n-3];
var ext;
var root;
if(second2l.length <= 3){//if google.com.ph, or bbc.co.uk
ext = second2l +"."+ last;
root = third2l;
}else{// adsense.google.com
ext = last;
root = second2l;
}
var domain = ""+ root + "." + ext;
return domain;
}
},
//private
isNumber : function (o) {
return !isNaN (o-0);
},
//private
/**
* determines if the url is an ip address
*/
isIp: function(domain){
var exploded = domain.split('.');
for(var i = 0; i < exploded.length; i++){
if(!UriParser.isNumber(exploded[i])){
return false;
}
}
return true;
},
isSameDomain: function(url1, url2){
if(UriParser.parseRootDomain(url1) == UriParser.parseRootDomain(url2)){
return true;
}
return false;
}
}
</script>
Step 2: Copy & Paste HTML code below in your BODY section
HTML
Code:
<h3>Usages:</h3>
document.write(UriParser.parseRootDomain("http://user:password@www.truste.com.ca:80/pathname?querystring&key=value#fragment"));<br />
document.write(UriParser.parseRootDomain("http://google.com.ph/pathname?querystring&key=value#fragment"));<br />
document.write(UriParser.parseRootDomain("http://www.google.com/pathname?querystring&key=value#fragment"));<br />
document.write(UriParser.parseRootDomain("http://google.com/pathname?querystring&key=value#fragment"));<br />
document.write(UriParser.parseRootDomain("http://localhost/pathname?querystring&key=value#fragment"));<br />
document.write(UriParser.parseRootDomain("localhost"));<br />
document.write(UriParser.parseRootDomain("http://192.168.1.1/pathname?querystring&key=value#fragment"));<br />
document.write(UriParser.isSameDomain("http://www.google.com/pathname?querystring&key=value#fragment", "http://adsense.google.com/?x=123123"))
document.write(UriParser.isSameDomain("http://www.blogger.com/navbar.g?targetBlogID=5856931630868336061&blogName=TeamPilipinas.info&publishMode=PUBLISH_MODE_HOSTED&navbarType=BLACK&layoutType=LAYOUTS&searchRoot=http%3A%2F%2Fteampilipinas.info%2Fsearch&blogLocale=en_PH&homepageUrl=http%3A%2F%2Fteampilipinas.info%2F", "blogger.com"));<br />
<hr />
<h3>Results:</h3>
<script type="text/javascript">
document.write(UriParser.parseRootDomain("http://user:password@www.truste.com.ca:80/pathname?querystring&key=value#fragment") + '<br />');
document.write(UriParser.parseRootDomain("http://google.com.ph/pathname?querystring&key=value#fragment") + '<br />');
document.write(UriParser.parseRootDomain("http://www.google.com/pathname?querystring&key=value#fragment") + '<br />');
document.write(UriParser.parseRootDomain("http://google.com/pathname?querystring&key=value#fragment") + '<br />');
document.write(UriParser.parseRootDomain("http://localhost/pathname?querystring&key=value#fragment") + '<br />');
document.write(UriParser.parseRootDomain("localhost") + '<br />');
document.write(UriParser.parseRootDomain("http://192.168.1.1/pathname?querystring&key=value#fragment") + '<br />');
document.write(UriParser.isSameDomain("http://www.google.com/pathname?querystring&key=value#fragment", "http://adsense.google.com/?x=123123"))
document.write(UriParser.isSameDomain("http://www.blogger.com/navbar.g?targetBlogID=5856931630868336061&blogName=TeamPilipinas.info&publishMode=PUBLISH_MODE_HOSTED&navbarType=BLACK&layoutType=LAYOUTS&searchRoot=http%3A%2F%2Fteampilipinas.info%2Fsearch&blogLocale=en_PH&homepageUrl=http%3A%2F%2Fteampilipinas.info%2F", "blogger.com"));
</script>