0

I have a javascript file that has two global variables. The problem is that when I minify the file the variables are removed and I get errors in the console.

How can I minify the file and have it keep my global variables.

/** global variables **/
window.$url = 0;
window.$searchText = 0;
Grady D
  • 1,749
  • 6
  • 28
  • 58

1 Answers1

2

All global variables in JavaScript are, in fact, properties of the window object.

Instead of setting a global variable like:

var global_name = 2;

you can set it as:

window.global_name = 2; /* no "var" */

and then retrieve it in the usual manner.

Better yet, namespace your global variables inside another object to prevent other scripts from accidentally tripping over them:

window.namespace_name.global_name = 2;
/* make all your global vars properties of window.namespace_name */
Community
  • 1
  • 1
Blazemonger
  • 86,267
  • 25
  • 136
  • 177