If you're just looking to keep from polluting the global namespace, you can
wrap all of your code in a closure. Lots of info here:
http://www.google.com/search?q=javascript+closures

For example (untested):

(function() {
  var ele = '#foo';
  $(document).ready(function() {
       $(ele). // ... Do something with 'ele'...
  });
  $(window).load(function () {
       $(ele). // ... Do something with 'ele'...
  });
})();

Then ele will just be available for the scope of your closure. Also, if you
really are doing something like your example, you might as well cache the
jQuery object instead of the selector to save having to look it up twice. A
good convention is to start variable names that point to a jQuery object
with $, like so:

(function() {
  var $ele = $('#foo');
  $(document).ready(function() {
       $ele. // ... Do something with 'ele'...
  });
  $(window).load(function () {
       $ele. // ... Do something with 'ele'...
  });
})();

Hope it helps.

--Erik


On 12/13/07, Micky Hulse <[EMAIL PROTECTED]> wrote:
>
>
> Just wondering what would be the best way to handle shared variable(s)
> between different events... for example:
>
> var ele = '#foo';
> $(document).ready(function() {
>         $(ele). // ... Do something with 'ele'...
> });
> $(window).load(function () {
>         $(ele). // ... Do something with 'ele'...
> });
>
> To me, the above seems a little sloppy... Is there a good way to
> contain the variable "ele" within it's own (relevant) namespace?
>
> Maybe I should be learning/reading about classes (OOP) and jQuery?
>
> Am I thinking too hard about this? :D
>
> I would greatly appreciate tips and/or suggestions.
>
> Have a great day/night!
> Cheers,
> Micky
>

Reply via email to