0

Is there an easy way to fetch a site name from a URL string?

Example:

http://www.mysite.com/mypath/mypage -> www.mysite.com
http://mysite.com/mypath/mypage     -> mysite.com

The JS code is executed on mongodb CLI side, not in a browser.

BreakPhreak
  • 10,940
  • 26
  • 72
  • 108
  • Can I ask, why is this being executed in the MongoDB CLI? Would it not be more prudent to do this on document insert in node.js? Or on querying it? – Sammaye Jan 06 '13 at 19:58
  • It's a part of a `group` query in MongoDB. – BreakPhreak Jan 07 '13 at 09:42
  • Can you show us what you have for this group query atm? It sounds good doing it this way, more that if you were querying (grouping) on the site name often you would want to save the document with the site name pre-regexed out since even though you can, in the aggregation framework, regex out parts it could never use an index which would be dirt slow – Sammaye Jan 07 '13 at 09:55
  • http://stackoverflow.com/questions/14182973/how-to-group-by-specifying-a-function-to-fetch-a-key - please see the marked answer (and my comment under it) – BreakPhreak Jan 07 '13 at 17:41
  • 1
    that group is old and slow plus it won't work on sharded collections and takes full read lock from other operations while running ( http://docs.mongodb.org/manual/reference/commands/#group ) but yea I see your not using that regex as a condition which should be ok. – Sammaye Jan 07 '13 at 17:47

6 Answers6

3

Try this:

url.href = "http://www.mysite.com/mypath/mypag";

url.protocol; // => "http:"
url.hostname; // => "example.com" // site name, you want

The window.document.location.href has the url of the page, and the window.document.location.hostname will have the sitename.

So,

console.log(window.document.location.hostname); // will log the sitename in the console
Viren Rajput
  • 5,426
  • 5
  • 30
  • 41
1

Easy.

window.location.hostname; //Domain name

$("title").text(); //Page name

and this too

var loc = window.location;

var filename = loc.pathname.split("/");
filename = filename[pathname.length-1];

alert("Domain: "+loc.hostname);
alert("Filename: "+filename);
Saransh Sharma
  • 308
  • 5
  • 19
0

You can abuse an anchor in order to get your desired data out of an URL. :D

function getDomain(url) {
  var anchor = document.createElement('a');
  anchor.setAttribute('href', url);

  return anchor.hostname;
}

console.log(getDomain('http://www.mysite.com/mypath/mypage'));
console.log(getDomain('http://mysite.com/mypath/mypage'));

http://jsfiddle.net/Js76M/

Robin Drexler
  • 4,307
  • 3
  • 25
  • 28
  • the question is about server-side JS (mongodb/node.js etc) - my question was edited by someone else, rolled back – BreakPhreak Jan 06 '13 at 17:18
0

This one worked well for me:

urlString.split('/')[2]

BreakPhreak
  • 10,940
  • 26
  • 72
  • 108
0

Try this:

var sitename = new URL(site_url)?.hostname?.replace("www.", "")
console.log(sitename)
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
-1

I also need to get domain from a url to show only domain name on a reference section of my app.

I found this here: https://developer.mozilla.org/en-US/docs/Web/API/URL/URL

The URL() constructor returns a newly created URL object representing the URL defined by the parameters.

If the given base URL or the resulting URL are not valid URLs, the JavaScript TypeError exception is thrown.

function getDomain(url) {
   return new window.URL(url).hostname;
}

new URl(url) returns an object with the follows properties

hash: ""
host: "developer.mozilla.org"
hostname: "developer.mozilla.org"
href: "https://developer.mozilla.org/en-US/docs/Web/API/URL/URL"
origin: "https://developer.mozilla.org"
password: ""
pathname: "/en-US/docs/Web/API/URL/URL"
port: ""
protocol: "https:"
search: ""
Alex Alan Nunes
  • 174
  • 2
  • 9