Skip to content

Instantly share code, notes, and snippets.

@bluesid
Created January 4, 2024 04:23
Show Gist options
  • Save bluesid/847d55fa97885b130fd81055b3b4adb0 to your computer and use it in GitHub Desktop.
Save bluesid/847d55fa97885b130fd81055b3b4adb0 to your computer and use it in GitHub Desktop.
#domain #currentdomain #js #javascript
// https://stackoverflow.com/questions/11401897/get-the-current-domain-name-with-javascript-not-the-path-etc
function getDomain(url, subdomain) {
subdomain = subdomain || false;
url = url.replace(/(https?:\/\/)?(www.)?/i, '');
if (!subdomain) {
url = url.split('.');
url = url.slice(url.length - 2).join('.');
}
if (url.indexOf('/') !== -1) {
return url.split('/')[0];
}
return url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment