@Steve I completely agree with everything you have written in your previous post. My point, and solution to the original question, was about having "var" when declaring a variable in the *global* scope (*not* in any function). I didn't mean to suggest to "var" any variable within an addEvent function.
(I was replying to your comment "no i wouldnt do that" when I suggested declaring the variable with "var" in the global scope) But did you mean that you shouldn't have global variables, as a point of programming standards? With this point I would also agree, I would try to minimise the number of global variables, say by wrapping things up in objects. Michal. On Dec 17, 3:36 pm, "Steve Onnis" <[email protected]> wrote: > Thats because at that point the variable "test" belongs to the document and > because its set on the document level anything can have access to it. > > Where as take this for example... > > <script> > > function fooOne () { > var fooOneVar = "hello from fooOne"; > } > > function fooTwo () { > alert(fooOneVar); > } > > fooTow(); > > </script> > > This will produce an error because "fooOneVar" becomes a static value > "inside" the fooOne() function. > > In essence i am pretty sure this will also be the case if you were to var a > variable inside the addEvent function because all you are doing is adding > functions to the domReady property. > > Steve > > -----Original Message----- > From: [email protected] > > [mailto:[email protected]] On Behalf Of Michal > Sent: Thursday, 18 December 2008 2:21 AM > To: MooTools Users > Subject: Re: IE(6) and variables in domready > > @Steve: > > I have also just done a small test: > > http://paste.mootools.net/f49801ffb > > and the code alerts a value of "3" from the "var test" variable, so > I'm not sure if your point was right. > > Michal. > > On Dec 17, 3:15 pm, Michal <[email protected]> wrote: > > @Steve: > > > I agree that there shouldn't be a "var mainMenu" in a function scope > > if it is to be used elsewhere. However, I've had a quick search, and > > from what I've found if you "var" something in global scope it becomes > > global (and thus available in all other scopes, such as in a domready > > function). Perhaps I am still confused... > > > Michal. > > > On Dec 17, 2:29 pm, stratboy <[email protected]> wrote: > > > > Yes Michael, that's a solution I think. I was just trying a similar > > > thing adding mainMenu as a window property and it worked: > > > > window.addEvent('domready', function() { > > > window.mainMenu = new Menu({ menuId:'menu', type:'bar', > > > props:mainMenu }); > > > > }); > > > > anyway, probably your suggestion is more efficient, couse that way I > > > don't touch the window object :) > > > > Anyway, the thing that still remains quite strange to me, is that the > > > first version I posted worked right in Safari and FF, and the problem > > > is only with IE. Mmm... Maybe it's not a thing so strange.. > > > > On 17 Dic, 15:22, Michal <[email protected]> wrote: > > > > > Perhaps I've got the var thing a bit confused, but I would put > > > > > var mainMenu; > > > > > in the global scope, and then all later references to mainMenu should > > > > refer to that. > > > > > Michal. > > > > > On Dec 17, 2:12 pm, "Steve Onnis" <[email protected]> wrote: > > > > > > silly question.... > > > > > > which file is being included first? > > > > > > -----Original Message----- > > > > > From: [email protected] > > > > > > [mailto:[email protected]] On Behalf Of stratboy > > > > > Sent: Thursday, 18 December 2008 1:11 AM > > > > > To: MooTools Users > > > > > Subject: IE(6) and variables in domready > > > > > > Hi! I've got a little problem with domready in ie6: > > > > > > window.addEvent('domready', function() { > > > > > //no var key, so mainMenu should be global > > > > > mainMenu = new Menu({ menuId:'menu', type:'bar', > > > > > props:mainMenu }); > > > > > }); > > > > > > later, in another .js, file > > > > > > window.addEvent('domready', function() { > > > > > mainMenu.setStartButton({ mainIndex:6, subIndex:6, > > > > > openSub:true }); > > > > > }); > > > > > > Safari and Firefox are just happy, but IE6 (don't know IE7) says > that > > > > > mainMenu is not defined. > > > > > > This seems to be a mootools 1.2.1 issue, since in another site using > > > > > 1.2.0, the same classes with the same methods work fine in IE6. > > > > > > Any idea? Is that maybe the two domready don't execute in sequence? > > > > > > If can be of help, I can post links to pages. > > > > > > Bye!
