Merge 2 jQuery functions -


i have 2 jquery functions basicly written same except 1st lines:

$(document).ready(function() {  var h = $(window).height();  var w = $(window).width();  $('#foo').css({'height' : h});  $('#foo').css({'width' : w}); });  $(window).resize(function() {  var h = $(window).height();  var w = $(window).width();  $('#foo').css({'height' : h});  $('#foo').css({'width' : w}); }); 

so wondering if possible merge them 1 how?

as in advance.

you can trigger resize event manually:

$(function() { // shortcut $(document).ready(function() {...});     $(window).resize(function() {   // bind handler         $('#foo').css({             'height': $(window).height(),             'width': $(window).width()         });     }).resize(); // trigger resize event }); 

or create function , bind both events:

function resizehandler() {     $('#foo').css({         'height': $(window).height(),         'width': $(window).width()     }); }  $(document).ready(resizehandler); $(window).resize(resizehandler); 

btw. $(function(){...}); shortcut $(document).ready(function(){...});.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -