Global javascript variable inside document.ready

前端 未结 8 1876
感情败类
感情败类 2020-12-08 04:04

Which is the right way of declaring a global javascript variable? The way I\'m trying it, doesn\'t work

$(document).ready(function() {

    var intro;

    i         


        
相关标签:
8条回答
  • 2020-12-08 04:41

    like this: put intro outside your document ready, Good discussion here: http://forum.jquery.com/topic/how-do-i-declare-a-global-variable-in-jquery @thecodeparadox is awesomely fast :P anyways!

     var intro;
    
    $(document).ready(function() {
    
    
    
        if ($('.intro_check').is(':checked')) {
            intro = true;
            $('.intro').wrap('<div class="disabled"></div>');
        };
    
        $('.intro_check').change(function(){
            if(this.checked) {
                intro = false;
                $('.enabled').removeClass('enabled').addClass('disabled');
            } else {
                intro = true;
                if($('.intro').exists()) {
                    $('.disabled').removeClass('disabled').addClass('enabled'); 
                } else {
                    $('.intro').wrap('<div class="disabled"></div>');
                }
            }
        });
    });
    
    0 讨论(0)
  • 2020-12-08 04:45

    Unlike another programming languages, any variable declared outside any function automatically becomes global,

    <script>
    
    //declare global variable
    var __foo = '123';
    
    function __test(){
     //__foo is global and visible here
     alert(__foo);
    }
    
    //so, it will alert '123'
    __test();
    
    </script>
    

    You problem is that you declare variable inside ready() function, which means that it becomes visible (in scope) ONLY inside ready() function, but not outside,

    Solution: So just make it global, i.e declare this one outside $(document).ready(function(){});

    0 讨论(0)
  • 2020-12-08 04:50

    Use window.intro inside of $(document).ready().

    0 讨论(0)
  • 2020-12-08 04:51

    declare this

    var intro;
    

    outside of $(document).ready() because, $(document).ready() will hide your variable from global scope.

    Code

    var intro;
    
    $(document).ready(function() {
        if ($('.intro_check').is(':checked')) {
            intro = true;
            $('.intro').wrap('<div class="disabled"></div>');
        };
        $('.intro_check').change(function(){
            if(this.checked) {
                intro = false;
                $('.enabled').removeClass('enabled').addClass('disabled');
            } else {
                intro = true;
                if($('.intro').exists()) {
                    $('.disabled').removeClass('disabled').addClass('enabled'); 
                } else {
                    $('.intro').wrap('<div class="disabled"></div>');
                }
            }
        });
    });
    

    According to @Zakaria comment

    Another way:

    window.intro = undefined;
    
    $(document).ready(function() {
        if ($('.intro_check').is(':checked')) {
            window.intro = true;
            $('.intro').wrap('<div class="disabled"></div>');
        };
        $('.intro_check').change(function(){
            if(this.checked) {
                window.intro = false;
                $('.enabled').removeClass('enabled').addClass('disabled');
            } else {
                window.intro = true;
                if($('.intro').exists()) {
                    $('.disabled').removeClass('disabled').addClass('enabled'); 
                } else {
                    $('.intro').wrap('<div class="disabled"></div>');
                }
            }
        });
    });
    

    Note

    console.log(intro);
    

    outside of DOM ready function (currently you've) will log undefined, but within DOM ready it will give you true/ false.

    Your outer console.log execute before DOM ready execute, because DOM ready execute after all resource appeared to DOM i.e after DOM is prepared, so I think you'll always get absurd result.


    According to comment of @W0rldart

    I need to use it outside of DOM ready function

    You can use following approach:

    var intro = undefined;
    
    $(document).ready(function() {
        if ($('.intro_check').is(':checked')) {
            intro = true;
            introCheck();
            $('.intro').wrap('<div class="disabled"></div>');
        };
        $('.intro_check').change(function() {
            if (this.checked) {
                intro = true;
            } else {
                intro = false;
            }
            introCheck();
        });
    
    });
    
    function introCheck() {
        console.log(intro);
    }
    

    After change the value of intro I called a function that will fire with new value of intro.

    0 讨论(0)
  • 2020-12-08 04:55

    You can define the variable inside the document ready function without var to make it a global variable. In javascript any variable declared without var automatically becomes a global variable

    $(document).ready(function() {
        intro =  "something";
    });
    

    although you cant use the variable immediately, but it would be accessible to other functions

    0 讨论(0)
  • 2020-12-08 05:01

    Use window.intro = "value"; inside the ready function. "value" could be void 0 if you want it to be undefined

    0 讨论(0)
提交回复
热议问题