[Dojo-interest] global variable value not retained within load
function
Meng-Shyang Teng
mteng.sub at gmail.com
Mon Feb 5 09:11:41 MST 2007
Jim,
Thanks for the prompt reply. Here's my code in its entirety:
Notice I did not re-declare trunkGroup inside of the function. If in-line
JavaScript is executed as the page is parsed
top-to-bottom, then shouldn't calling the function getTrunkGroups() before
the body tag generate the correct value for trunkGroup?
Please advise.
<html>
<head>
<title>Example</title>
<script language="javascript" src="js/dojo/dojo.js"></script>
<script language="javascript">
dojo.require("dojo.io.*");
dojo.require("dojo.event.*");
var trunkGroup = new Array();
function getTrunkGroups() {
var params = new Array();
params['cfgType']="AscendentTrunkCfg";
params['cmd']="get";
params['xquery']="//TRUNK_RESERVE_METHOD/TrunkGroup";
params['cache']="no";
var bindArgs = {
url: "/xtools/servlet/processXMLCfg",
error: function(type, data, evt){
alert(data);
},
load: function(type, data, evt){
trunkGroup = eval(data);
alert(trunkGroup[0]); // this outputs just fine
},
mimetype: "text/json",
content: params
};
var requestObj = dojo.io.bind(bindArgs);
}
getTrunkGroups();
</script>
</head>
<body>
<p>
<script language="javascript">
document.write(trunkGroup[0]); // this outputs "undefined"
</script>
</body>
</html>
On 2/5/07, Jim <openbip at gmail.com> wrote:
>
> 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
> >
>
> _______________________________________________
> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://dojotoolkit.org/pipermail/dojo-interest/attachments/20070205/5d4ae34d/attachment.html
More information about the Dojo-interest
mailing list