Javascript startsWith() and check the domain of executed script using window.location.hostname

Just to remind myself… there is no String.startsWith() in Javascript. This bite me once again… Instead you do a regular expression match from the beginning of the string.

To find out whether your Javascript is being executed on a certain domain you do:

if(window.location.hostname).match("^opensourcehacker.com")) {
   // yes this is our domain
} else {
   // subdomain (might be www.!) or some else domain
}

Yours sincerely, The regex hater

\"\" Subscribe to RSS feed Follow me on Twitter Follow me on Facebook Follow me Google+

2 thoughts on “Javascript startsWith() and check the domain of executed script using window.location.hostname

  1. Thanks for this! Worked great for me but I wanted to comment for others that a “(” is missing in your if statement. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *