Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery combine .ready and .resize functions [duplicate]

Tags:

jquery

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!

like image 779
Stickers Avatar asked Oct 31 '11 15:10

Stickers


2 Answers

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);
like image 98
JaredPar Avatar answered Nov 11 '22 06:11

JaredPar


$(window).on('resize',function() {

}).trigger('resize');
like image 34
davidcondrey Avatar answered Nov 11 '22 05:11

davidcondrey