[Dojo-interest] global variable value not retained within load
function
Jim
openbip at gmail.com
Mon Feb 5 08:47:45 MST 2007
Could you paste your entire page? It's difficult to tell if you have a
scoping issue or a timing issue.
Remember that in-line JavaScript is executed as the page is parsed
top-to-bottom, and /then/ any onload functionality will be executed ...
so if you're initializing trunkGroup inside dojo.addOnLoad(...), that
initialization would not happen until after your document.write(...)
call below.
<body>
<script language="javascript">
document.write(trunkGroup[0]); // outputs "undefined"
</script>
</body>
I can tell you that the following executes "array" "string" "string",
showing that you can indeed manipulate global variables in more-specific
scopes.
var bip = "array";
function bip2 () {
this.bip3 = function() {
bip = "string";
alert(bip);
}
};
dojo.addOnLoad(
function() {
alert(bip);
new bip2().bip3();
alert(bip);
}
);
One final note, using "var" declares that variable specifically to its
containing scope. So if you're doing:
dojo.addOnLoad(
function() {
var trunkGroup ...
});
... then "trunkGroup" is not going to be global, but a private
member-variable of the anonymous class you're passing into the addOnload
function.
-- Jim
Meng-Shyang Teng wrote:
> Within the load function, I have the following:
>
> var trunkGroup = new Array(); // global variable defined here
> function getTrunkGroups() {
> .. // some code omitted here
>
> var bindArgs = {
> load: function(type, data, evt){
> trunkGroup = eval(data); // output correct value
> alert (trunkGroup[0]);
> }
> }
> ..
> }
>
>
> I am attempting to assign a global variable trunkGroup. Although the
> alert(trunkGroup[0]) does return correct values, the trunkGroup is no
> longer defined within the html document. Is there a way to assign a
> global variable inside of the load function?
>
> I have the following code inside of my html:
>
> <body>
> <script language="javascript">
> document.write(trunkGroup[0]); // outputs "undefined"
> </script>
> </body>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Dojo FAQ: http://dojo.jot.com/FAQ
> Dojo Book: http://manual.dojotoolkit.org/DojoDotBook
> Dojo-interest at dojotoolkit.org
> http://dojotoolkit.org/mailman/listinfo/dojo-interest
>
More information about the Dojo-interest
mailing list