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,889
  • 6
  • 30
  • 61

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
  • 90,923
  • 26
  • 142
  • 180