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.
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
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);
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'));
This one worked well for me:
urlString.split('/')[2]