最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Global Variable JavaScript (Changing Value) - Stack Overflow

programmeradmin0浏览0评论

Is it possible to change the value of a global variable in JavaScript?

If so, is it possible to do it in a function called by an event listener such as "onreadyStateChange"?

It's working for normal functions. but doesn't change when I call a function like this:

<script.......>
    var dom1 = 3;

    function work()
    {
        ...
        http.onreadyStateChange=handleHttpResponse;
        ...
    }

    function handleHttpResponse()
    {
        var xd;
        if (http.readyState == 4)
        {
            if (http.status == 200)
            {
                if (http.responseText == "granted")
                {
                    dom1 = 1;
                }
                else
                {
                    dom1 = 2;
                }
            }
            else
            {
                alert("Error");
            }
        }
    }
</script>

Is it possible to change the value of a global variable in JavaScript?

If so, is it possible to do it in a function called by an event listener such as "onreadyStateChange"?

It's working for normal functions. but doesn't change when I call a function like this:

<script.......>
    var dom1 = 3;

    function work()
    {
        ...
        http.onreadyStateChange=handleHttpResponse;
        ...
    }

    function handleHttpResponse()
    {
        var xd;
        if (http.readyState == 4)
        {
            if (http.status == 200)
            {
                if (http.responseText == "granted")
                {
                    dom1 = 1;
                }
                else
                {
                    dom1 = 2;
                }
            }
            else
            {
                alert("Error");
            }
        }
    }
</script>
Share Improve this question edited Feb 28, 2010 at 5:50 Matthew Crumley 103k24 gold badges104 silver badges131 bronze badges asked Feb 28, 2010 at 4:28 muntasirmuntasir 111 gold badge1 silver badge2 bronze badges 1
  • 1 I always try to be cautious when introducing global variables to avoid conflicts with existing or future code, what are you trying to do? – Christian C. Salvadó Commented Feb 28, 2010 at 4:50
Add a comment  | 

3 Answers 3

Reset to default 7

You can change the value of any variable in JS, local or global. In a function, make sure you don't declare a local variable with the same name and you can access the global. Even if you do declare a local, you can access the global as a property of window. You can change most properties as well; there are very few immutable data types in JS or the DOM.

If a variable isn't being set as you expect, you can use Firefox and firebug to debug the code and observe what's happening.

Please use window['dom1'] = xxx; instead of var dom1 = xxx;

Please try:

<script type="text\javascript"> 
    var dom1 = 3; 

    function work() 
    { 
        ... 
        http.onreadyStateChange=handleHttpResponse; 
        ... 
    } 

    function handleHttpResponse() 
    { 
        var xd; 
        if (http.readyState == 4) 
        { 
            if (http.status == 200) 
            { 
                if (http.responseText == "granted") 
                { 
                    *window['dom1']* = 1; 
                } 
                else 
                { 
                    *window['dom1']* = 2; 
                } 
            } 
            else 
            { 
                alert("Error"); 
            } 
        } 
    } 
</script>

You would find the global value "dom1" is finally changed!

发布评论

评论列表(0)

  1. 暂无评论