80

Currently I am working on a legacy web page that uses a ton of JavaScript, jQuery, Microsoft client JavaScript, and other libraries. The bottom line - I cannot rewrite the entire page from scratch as the business cannot justify it. So... it is what it is. Anyway, I need to pollute (I really tried not too) the global namespace with a variable. There are the three options I was thinking about -

  1. Just store/retrieve it using a normal JavaScript declaration - var x = 0;

  2. Use jQuery to store/retrieve the value in a DOM tag - $("body").data("x", 0);

  3. Use a hidden form field, and set/retrieve the value with jQuery - $("whatever").data("x", 0);

Is there a better way? I looked at the existing pile of code, and I do not believe the variable can be scoped in a function.

user2864740
  • 60,010
  • 15
  • 145
  • 220
Kris Krause
  • 7,304
  • 2
  • 23
  • 26

6 Answers6

100

You can create a namespace inside the jQuery object, like so:

$.mynamespace = { 
    myVar : "something", 
    myVar2 : "somethingElse" 
}; 

or:

$.mynamespace = {};
$.mynamespace.myVar = "something";
$.mynamespace.myVar2 = "somethingElse";

Bear in mind, any plugin method named 'mynamespace' will be overwritten so be sure to use a sensible name.

karim79
  • 339,989
  • 67
  • 413
  • 406
  • 1
    why should the namespace variable declared as an attribute of jQuery?! – Pedro Rolo May 19 '10 at 15:36
  • 5
    @Pedro - It does not need to be. I was just pointing this out as one possibility. – karim79 May 19 '10 at 15:39
  • If I want to use this, where have I to declare it? All my javascript code is in a JS file. I don't want to mix JavaScript code with HTML code. – VansFannel May 29 '11 at 10:21
  • this is a silly solution that it is prone to name clashes between the declared namespaces and the variables and methods of the jQuery Object. I don't understand the value of this solution. For me, It isn't good. – Pedro Rolo Sep 04 '11 at 19:30
  • i can use this global variable across all iframe form parent? – Thulasiram Oct 17 '12 at 10:52
  • I can see how this keeps the global namespace sparse because your custom namespace goes inside the jQuery namespace, but I have no problem creating another uniquely-named global namespace (such as myAppSettings) to contain all the globals related to an entire application. To me it makes more sense also because it isn't dependent on having jQuery there. – JD Smith Jan 28 '13 at 18:30
  • How would you put in a variable where "something" is currently written? I've tried passing myVar and does not want to console or alert out http://stackoverflow.com/questions/16327545/not-able-to-get-namespace-variable-to-alert-in-jquery-function – Leon Gaban May 02 '13 at 14:44
40

For me the best way to handle this situation is to define an object in the window object:

window.my_config =
{
    my_var1 : 1,
    my_var1 : 2,
    my_var1 : 3
};

This would keep your scope neat and clean. And whenever you would access the global using window.my_config anyone looking at the code would know that a global is being accessed.

ovais.tariq
  • 2,627
  • 17
  • 12
  • 4
    this makes more sense to me than using jquery namespace. Is there any reason this way would not work just as well or better? – Damon Dec 09 '11 at 20:45
6

You can create a hash in the global scope and use it as a namespace:

MyNamepace={}
MyNamespace.newvar = 'value'
// MyNamespace.newvar => 'value'
Pedro Rolo
  • 28,273
  • 12
  • 60
  • 94
  • This is a silly solution that it is prone to name clashes between the declared namespaces and other global variables and methods. I don't understand the value of this solution. For me, It sucks. – tad Sep 06 '11 at 21:42
  • 1
    You do have control over which names exist in the global scope, while you don't have control over the methods and variable names that might be added to jQuery on update. This is one of the strong reasons why namespaces do explicitly exist in many languages. When they do not, names start to be strange (i.e.: Smalltalk-systems class-names) – Pedro Rolo Sep 07 '11 at 13:19
  • 4
    Not always. There's nothing stopping some rogue library from overriding your hash. In essence, you can't avoid the problem _without_ explicit namespacing. This is why the jQuery module authoring documentation recommends a single hash in the jQuery object for namespacing purposes, because the jQuery namespace is _de facto_ restricted to popular plugins. – tad Sep 07 '11 at 22:28
2

Just sharing my practice with you, I would make a global object/var in the required JavaScript file with a sensible prefix, as in if I am working on a page where this object will be a text box then I would name it:

g_TxtMyValue = 'value';    // g_ specifies it to be a global variable, it is one
                           // of the many conventions used

If you have more than one global variable, you can also have a namespace such as:

my_txt = {};  // For a real site I would use a prefix relative to the project
              // name instead of "my".

my_txt.testValueOne = 'Value one';
my_txt.testValueOne = 'Value two';

These variables will be available to you throughout the website, after they have been initialized.

I hope this helps.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Umair Jabbar
  • 3,596
  • 5
  • 30
  • 42
0

Just a short notice:

Is the fancybox doing AJAX (meaning: if it loads within an iFrame, you should add "parent" to the close method), like this:

parent.$.fancybox.close();
BananaAcid
  • 3,221
  • 35
  • 38
Moriz
  • 11
  • 2
0

Use underscore isEmpty().

_.isEmpty('') will return true.

user3335966
  • 2,673
  • 4
  • 30
  • 33
Eldad
  • 11
  • 1