Jump to content

L'il tip regarding jQuery, getScript, and Global vars


jcanker

Recommended Posts

Still pretty new, but just figured this out (and you ol' timers can feel free to slap me for the noob error).  I'm posting it here in case someone else runs into the same issue, and to remind ourselves of some of the basics we can easily forget.

 

If you're using jquery, then you're using,

$(document).ready(function() {
});

and placing your code inside the function here so that it doesn't run until the page is loaded and ready to roll.

 

Any variables you declare in here are NOT global--you'll still have to pass the variable object to functions, etc.  I found this because $.getScript wasn't seeing the object literal.

 

Declare the variable /object literal *before* your $(document).ready(function){}) and then it will truly be global:

var myObjectLiteral = {};
$(document).ready(function() {
     //code here, and you can mess with myObjectLiteral all you want now!

});

 

The beatings by veterans may now commence  :shrug:

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.