Set Production Flag in JavaScript Based on Domain Extension

In a recent project I wanted to be able to dynamically set a production flag in JavaScript. Doing so would allow me to keep my production analytics free from dev and staging environment events. Because my dev and staging environments don’t run on a .com domain extension I was able to go with this simple script

    var de = document.location.hostname.split('.');
    de = de[de.length - 1];
    window.prod = (de == 'com') ? true : false;

Then when I need code to only respond when in production it’s as easy as

    if(window.prod) {
        // do awesomeness
    }

Splitting up the domain in JavaScript to set the production flag has been working well and I’m quite happy with it so far. If you have suggestions or want to share what you have done, please share in the comments below.

Published by

MikeGrace

I am a Geek! I hope you enjoyed this recent post.