-3

Possible Duplicate:
Parse URL with Javascript

I want to compare the basic part of the URL. If I've entered www.websitename.com it should say duplicate if I enter

http://www.websitename.com
https://www.websitename.com
websitename.com
http://www.websitename.com/filename
subdomain.websitename.com etc.. 

How can I do it?

Community
  • 1
  • 1
Muhammad Usman
  • 10,426
  • 22
  • 72
  • 107
  • 2
    Why don't you cut off directories, and then do a string search for websitename? Am I missing a case you do not want to return true here? – thatidiotguy Aug 31 '12 at 17:56
  • 1
    Use a regex to get everything between "://" and ".com". Then compare the two strings normally. – Chase Aug 31 '12 at 17:57
  • Please take a look at https://developer.mozilla.org/en-US/docs/DOM/window.location – Teemu Aug 31 '12 at 18:04
  • 2
    :O look at the down-votes! *I don't mind what I'm typing in this comment this post is surely going to get deleted :P* – Robin Maben Aug 31 '12 at 18:07

1 Answers1

2

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/

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Please notice also that host names are not case sensitive (whild the path part may be case sensitive). – Leo Mar 09 '14 at 16:06
  • @Leo - good point. I added case insensitivity when looking for the `http://`. Comparing the returned domain would be the caller of this function's responsibility. – jfriend00 Mar 09 '14 at 17:28
  • `getDomain("http://foo.com:80")` -> `"foo.com:80"` – Atomosk Oct 11 '17 at 04:00
  • @Atomosk - What does your comment mean? Can you explain? – jfriend00 Oct 11 '17 at 04:43
  • For url like `http://domain:port` your method returns `domain:port`. `http://domain:80` and `http://domain` are basically the same thing, since 80 is default port for http and 443 is default for https. I think it's good to know for anyone who reads the answer. – Atomosk Oct 11 '17 at 13:26
  • @Atomosk - Made an edit to exclude the port from the match since the question only asks about domain. The OP did not indicate they wanted to actually parse and compare the port number. If so, that code could be added too, but at that point, I'd probably just get one of the popular URL parsing libraries that is already built. – jfriend00 Oct 11 '17 at 14:05