开发者

Javascript cross-scripts variable values

I think I am missing something here, but I really can't see what it i开发者_高级运维s. Shouldn't a variable declared outside a function be available throughout the whole page? What I'm trying here I have already seen before, so I guess the basic idea should work. In this case I always get the alert to tell me "false", so I guess it always enters if, thus skipUserDataEntry has undefined value instead of true. Hope someone can help.

    <script type="text/javascript">
     skipUserDataEntry = true;
    </script>

    <script type="text/javascript"> 
        Ext.onReady(function () {
         if (typeof skipUserDataEntry == "undefined")
      var skipUserDataEntry = false;
         alert (skipUserDataEntry);
        }
     </script>


A couple of problems. Your global variable is fine. But, any variable declared inside a function() ANYWHERE will be scoped for that WHOLE function.

This means your row reading var skipUserDataEntry = false; is actually declaring a new variable skipUserDataEntry for the function scope, shadowing the global one.

Remove the var and you will only look at the global scoped variable.

    Ext.onReady(function () {
     if (typeof skipUserDataEntry == "undefined")
       skipUserDataEntry = false;
     alert (skipUserDataEntry);
    });

Secondly you're missing an end parenthesis.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜