I’ve got following JavaScript functions but want to refactor the $(document).ready() as I’ve got 2 instance of it. 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);
First off, there’s no reason you have to combine them.
But if you want to:
Breaking it down:
You might choose to wrap each
initcall in atry/catchblock as well, so that errors in one init don’t prevent the next init from occuring, but that depends on your needs.