The reason you can't find any information about jQuery variables is that
jQuery doesn't have variables! :-)

You're talking about JavaScript, not jQuery. jQuery is not a language of its
own, it's just a library of JavaScript code that you can use in your own
JavaScript code.

If you look for information about JavaScript variables you will have much
better luck.

So about those JavaScript variables... Fortunately for all of us, you don't
have to use hidden input fields if you merely want to store data that will
persist for the lifetime of your page. An ordinary global variable works
fine for that.

Also, you don't have to use global variables. Even *local* variables can
easily outlive the function invocation that creates them. A trivial example:

// When the '#test' button is clicked,
// alert a number that increments each time

$(document).ready( testSetup );

function testSetup() {
    var i = 0;
    $('#test').click( function() {
        alert( ++i );
    });
}

Here I made testSetup() a separate named function instead of the anonymous
inline function that is more commonly used, just to make it clear that it *
is* a separate function.

testSetup() runs once and then returns immediately. What happens to the
variable i when the function returns? Does it go away? No, JavaScript uses a
*closure* to preserve this variable. Later, when you click the button, it
calls the click callback function that issues the alert. Note that this
inner function can still reference the i variable, and it can increment it
too. JavaScript keeps that variable in existence as long as it needs to.

Closures are a truly wonderful feature. Read up on them if you want a better
understanding of how a lot of jQuery and JavaScript code works.

So, is there ever a case where you *would* need to use a hidden input field
instead of a global or local variable? Yes! If you want to preserve data
even if the user hits the refresh button (or keyboard equivalent) to reload
the page. When that happens, all JavaScript code and data is wiped clean and
reloaded. If you want to preserve state in this situation, you can use a
hidden form field. Or depending on what you are trying to do, you can use
the hash fragment in your URL (the part after the #), or perhaps a cookie.

But to simply keep data around as long as your page is running and not
reloaded, global or local variables are all you need.

-Mike

On Sun, Sep 20, 2009 at 12:52 PM, Mad-Halfling <mad-halfl...@yahoo.com>wrote:

>
> Am I correct in thinking that variable in jquery are limited in scope
> by their parent functions, and if I want to persist data during a
> particular page's lifetime (thinking of a page that will exist for a
> while, being updated by AJAX calls, etc) I need to put it in an input
> control on that page - I am wanting to store data like sort-field for
> an AJAX updated data grid, so that data will need to be persistent and
> specific to that page, but I can't find any reference to global/page
> variables in jquery, so I was assuming I would need to have some
> hidden fields onthe page in which to store this data.
>
> Cheers
>
> MH
>

Reply via email to