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:

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.