If you just want to compare if two URLs have the same domain, you can easily code that with a regular expression.
There is no way for javascript to know that http://www.websitename.com/
and http://subdomain.websitename.com/
do or don't resolve to the same host or content without actually fetching the content and comparing it because it totally depends upon the host implementation which can be set either way and isn't something that javascript can know.
Here's a function that will get the domain from a URL:
function getDomain(url) {
var prefix = /^https?:\/\//i;
var domain = /^[^\/:]+/;
// remove any prefix
url = url.replace(prefix, "");
// assume any URL that starts with a / is on the current page's domain
if (url.charAt(0) === "/") {
url = window.location.hostname + url;
}
// now extract just the domain
var match = url.match(domain);
if (match) {
return(match[0]);
}
return(null);
}
You can see what it returns for each of your URLs here: http://jsfiddle.net/jfriend00/6YNgp/