jQuery: Global Variable Namespace Problem


Is there a way to declare a global variable in jQuery in its own namespace?

Sure, I can declare global variables with plain old JavaScript, but they will fall in the window's namespace. For example, if I had a variable named document, it would surely overwrite the document object of the window.

Does jQuery have a hash table that lets you store any object by their name?



Not sure why you want to use jQuery for that? You could just create your own namespace:

var namespace = { document : "Foo" };

Please elaborate, if there's some need to use jQuery somehow.


Of course you can. Create a namespace as an object and then attach it to the jQuery function.

(function ($) { $.myNamespace = {}; })(jQuery);

Here I've created an auto-executing anonymous function to do the work. I can then add other propertes and functions to the namespace inside this function. Also this avoids the problem of someone else renaming $ to something else.

After this is done you can refer to your namespace as jQuery.myNamespace


If the variable is something that want to associate with an element to be used later, maybe in a different routine, you could use jQuery's "data" method to store it.


: : // Save original HTML content. var old_html = $('#box').html(); $('#box').data({originalContent: old_html}); // Replace with new content. $('#box').empty().html(new_html); : // Restore original content (maybe in a different function). var orig_html = $('#box').data('originalContent'); $('#box').empty().html(orig_html); :

I hope this is useful to you.

Regards Neil


  • Serializing C# classes to MongoDB without using discriminators in subdocuments
  • Run all tests in namespace using Nunit3-console.exe
  • Magento Enterprise controller override
  • Connect to a password protected server with WCF over HTTP
  • What to use (best/good practice) for the secret key in HMAC solution?
  • How to search for a record and then delete it
  • bcrypt-nodejs compare method returns false every time
  • How to retrieve information from antrun back to maven?
  • For loop with if condition on multiple R functions
  • Hibernate to update table schema
  • Not able to aggregate on nested fields in elasticsearch
  • Get history of file changes from TFS to implement custom “blame”-behaviour of exceptions
  • Jackson Parser: ignore deserializing for type mismatch
  • Abort upload large uploads after reading headers
  • Rails Find when some params will be blank
  • Meteor helpers not available in Angular template
  • Change multiple background-images with jQuery
  • How do I change content of ComboFieldEditor?
  • Submit form in a displaytag pagination
  • Incrementing object id automatically JS constructor (static method and variable)
  • Trying to switch camera back to front but getting exception
  • Build own AppleScript numerical error handling
  • Weird JavaScript statement, what does it mean?
  • Do I've to free mysql result after storing it?
  • Delete MySQLi record without showing the id in the URL
  • Unanticipated behavior
  • Data Validation Drop Down Box Arrow Disappearing
  • Comma separated Values
  • How to delete a row from a dynamic generate table using jquery?
  • SQL merge duplicate rows and join values that are different
  • Error creating VM instance in Google Compute Engine
  • Free memory of cv::Mat loaded using FileStorage API
  • using HTMLImports.whenReady not working in chrome
  • Hits per day in Google Big Query
  • Trying to get generic when generic is not available
  • how does django model after text[] in postgresql [duplicate]
  • Authorize attributes not working in MVC 4
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • java string with new operator and a literal