0

I have an AngularJS 1.0.7 web app. I have a javascript plugin that I just want to execute in production environment (related with Google Analytics).

I have an ENVIRONMENT constant with different values according to environment, so my idea is to do something like:

<head>
<script ng-show="ENVIRONMENT == 'Production'">
  javascript code
</script>
</head>

or same with ng-switch.

However none of the seems to work, because I think they are rendered anyway, so finally the Google Analytics code is executed in every environment.

Rober
  • 5,868
  • 17
  • 58
  • 110

1 Answers1

0

Conditionally load JavaScript file

It's easy to do without dependencies on any js library.

if (ENVIRONMENT == 'Production')
{    
    var head = document.getElementsByTagName('head')[0];
    var js = document.createElement("script");    
    js.type = "text/javascript";
    js.src = "js/GoogleAnalitycs.js";
    head.appendChild(js);
}    
Community
  • 1
  • 1
IAfanasov
  • 4,775
  • 3
  • 27
  • 42