Set JavaScript variables based on hostname

Most of the websites I work on have at least two environments: staging (for testing) and production (the live site). Each environment has a different configuration, e.g. staging will typically have some form of debugging mode. In PHP I can usually work out which environment to use by examining the current file path, but I also need to do something similar for JavaScript.

Fortunately it is easy to set variables based on the hostname in JavaScript:

var debug_mode = ( === '')

if (debug_mode)
  console.log('debug mode is on');

The above code will enter a log message in the JavaScript console, but only if the code is executed on the staging site.


No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.