Refactor $ (document) .ready (), currently has 2 instances

I have the following JavaScript functions, but I want to reorganize $ (document) .ready () since I have two instances. How can I achieve this?

FlashMessenger = {
    init: function() {
        setTimeout(function() {
            $(".flash").fadeOut("slow", function () {
                $(".flash").remove();
            });
        }, 5000);
    }
}

SelectLanguage = {
    init: function() {
        $('#selectLanguageId').change(function() {
            $('#frmSelectLanguage').submit();
        });
    }
}

$(document).ready(FlashMessenger.init);
$(document).ready(SelectLanguage.init);
+3
source share
7 answers

Firstly, there is no reason why you should combine them.

But if you want:

$(document).ready(function(jq){
    FlashMessenger.init(jq);
    SelectLanguage.init(jq);
});

Destruction:

  • Create a function to perform all your initialization (it can be called either anonymous, and above - anonymously).
  • Ask him to call other init functions by passing a jQuery instance that jQuery passes to you just in case he uses it.

, init try/catch, init , .

+3

$(document).ready, , . , $() $(document).ready(handler):

$(FlashMessenger.init);
$(SelectLanguage.init);

, :

$(function() {
    FlashMessenger.init();
    SelectLanguage.init();
});
+8

:

$(document).ready(function()
{
  FlashMessenger.init();
  SelectLanguage.init();
});
+3
$(document).ready(function() {
    FlashMessenger.init();
    SelectLanguage.init();
});
+3

1

FlashMessenger = {
    init: function() {
        setTimeout(function() {
            $(".flash").fadeOut("slow", function () {
                $(".flash").remove();
            });
        }, 5000);
    }
}    
SelectLanguage = {
    init: function() {
        $('#selectLanguageId').change(function() {
            $('#frmSelectLanguage').submit();
        });
    }
}

$(function(){
    FlashMessenger.init();
    SelectLanguage.init();
});

2

FlashMessenger = {
    init: function() {
        setTimeout(function() {
            $(".flash").fadeOut("slow", function () {
                $(".flash").remove();
            });
        }, 5000);
    }
}

SelectLanguage = {
    init: function() {
        $('#selectLanguageId').change(function() {
            $('#frmSelectLanguage').submit();
        });
    }
}

$(document).ready(function(){
    FlashMessenger.init();
    SelectLanguage.init();
});

3
2 , init, , , .

$(function(){
    $('#selectLanguageId').change(function() {
        $('#frmSelectLanguage').submit();
    });
    setTimeout(function() {
        $(".flash").fadeOut("slow", function () {
                $(".flash").remove();
        });
    }, 5000);
})

2 3 .

+1

, op: " , document.ready, , ?"

$(document).ready(), startupHooks :

startupHooks[ startupHooks.length ] = myNewStartupHook;

script

$(document).ready(function() {
    for( var i=0; i<startupHooks.length; i++ ) {
        startupHooks[i]();
    }
}

, , , .

$(document).ready().

0

Personally, I would not use document.ready at all. If you put the scripts at the end of the html page (just before the tag), you can simply write as you like. It may not work for 0.01% of the scripts, but it never worked for me.

The positive effect of this is that the original HTML + CSS rendering is faster. You can also read about it on yahoo. http://developer.yahoo.com/performance/rules.html#js_bottom

0
source

Source: https://habr.com/ru/post/1765293/


All Articles