Pangloss Pangloss - 4 months ago 29
Javascript Question

jQuery combine .ready and .resize functions

As the title says, I have exactly the same code needs to run in both functions. Is there a way to put them together and run as one function please? Thanks!

Answer

Yes. Simply define a function with the common code and pass that as the argument to the two methods.

var callback = function () {
  // This is the common code
};

$(document).ready(callback);
$(window).resize(callback);