What is the best way to pass server side variables to JavaScript on the client side?
Posted
by
steve_c
on Stack Overflow
See other posts from Stack Overflow
or by steve_c
Published on 2011-01-04T20:43:38Z
Indexed on
2011/01/04
21:54 UTC
Read the original article
Hit count: 149
JavaScript
Our application uses a lot of configuration options. Those options need to be reflected on the client side in the form of User preferences, site wide preferences, etc.
Currently, we pass server side settings down to the client side in the form of JSON that is stored in custom attributes in the markup for a specific element (and no, our application currently doesn't worry about W3C validation). We then retrieve the data from the custom attribute, and parse it into a JSON object for use in script using jQuery.
One drawback to this is referencing attributes on elements from within event handlers. I know this is frowned upon, as it can create circular references, and subsequently memory leaks. I would much prefer to use jQuery's data function, but you can't invoke this from the server side at page render time.
What does everyone else do in this type of scenario?
© Stack Overflow or respective owner