Home How to store a global value (not necessarily a global variable) in jQuery?
Reply: 6

How to store a global value (not necessarily a global variable) in jQuery?

Kris Krause
1#
Kris Krause Published in 2010-05-19 15:20:59Z

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.

karim79
2#
karim79 Reply to 2010-05-19 15:31:08Z

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.

Pedro Morte Rolo
3#
Pedro Morte Rolo Reply to 2012-10-22 11:45:09Z

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

MyNamepace={}
MyNamespace.newvar = 'value'
// MyNamespace.newvar => 'value'
Peter Mortensen
4#
Peter Mortensen Reply to 2012-05-19 19:37:09Z

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.

ovais.tariq
5#
ovais.tariq Reply to 2010-05-24 08:46:34Z

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.

Moriz
6#
Moriz Reply to 2014-07-22 21:31:03Z

Just a short notice: Is the fancybox is AJAX (meaning it loads within an iFrame, you should add "parent" to the close method, like this: js parent.$.fancybox.close();

user3335966
7#
user3335966 Reply to 2016-01-22 01:17:03Z

Use underscore isEmpty().

_.isEmpty('') will return true.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.30524 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO