How to get domain name only using javascript?
Use location.host and cut off subdomains and the TLD: var domain = (location.host.match(/([^.]+)\.\w{2,3}(?:\.\w{2})?$/) || [])[1] update: as @demix pointed out, this fails for 2 and 3-letter domains. It also won’t work for domains like aero, jobs and dozens others. The only way around is to know valid TLDs in advance, so here is a more … Read more