jQuery global variable with getScript -


This object is decalred in foo.js, and has been modified in bar.js.

And when I getScript, I tried to get results.

foo.js

P>

  / * // It works fine, but I want to decalre it inside jQuery var myObject = {str: "hello world", num: "123"}; * / $ (Function () {// This makes my object global variable y myObject = {str: "Hello World", num: "123"}; Warning (myObject.str + "+ myObject.num); $ ; .getScript ("bar.js", function () {warning (myObject.str + "+ myObject.num);});});   

bar.js

  $ (function () {myObject = {str: "new string", number: " 999 "};});  

Since you have declared a variable within a function, it is local for functions . What you can do is declare the variable out, but assign values ​​inside jQuery's ready handler

foo.js

  / * // This works fine, but I want to decalre it within jQuery var myObject = {str: "hello world", num: "123"}; * / Var myObject; $ (Function () {//} This does not make my object global variable myoback {{str: "Hello World", num: "123"}; Alert (myObject.str + "+ myObject.num); $ .getScript ("Bar.js", function () {warning (myObject.str + "+ myObject.num);});});   

bar.js

  $ (function () {myObject = {str: "new string", number: " 999 "};});   

Demo:

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -