Helpful JavaScript for Web development
Simple JavaScript RegEx to Parse Domain Name
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: Place JavaScript below in your HEAD section
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: Place HTML 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>
JavaScript Enlarge Image -
JavaScript Fading Slide Show -
JavaScript Rotating Image script
Originally Posted by
Copyright
The Site may provide, or third parties may provide, links to non-JavaScriptBank.com Internet World Wide Web sites or resources. Because JavaScriptBank.com has no control over such sites and resources, you acknowledge and agree that JavaScriptBank.com is not responsible for the availability of such external sites or resources, and does not endorse and is not responsible or liable for any content, advertising, products, or other materials on or available from such sites or resources. You further acknowledge and agree that JavaScriptBank.com shall not be responsible or liable, directly or indirectly, for any damage or loss caused or alleged to be caused by or in connection with use of or reliance on any such content, goods or services available on or through any such site or resource.
Bookmarks