jQuery global variable best practice & options?

Posted by Kris Krause on Stack Overflow See other posts from Stack Overflow or by Kris Krause
Published on 2010-05-19T15:20:59Z Indexed on 2010/05/19 15:30 UTC
Read the original article Hit count: 450

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 -

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

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

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

What does everyone think? 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.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery