开发者

Is it safe to call jQuery's resize() function to execute my own resize handler?

I am defining a resize handler that positions some elements on the screen, like this:

$(window).resize(function() {
    // Position elements
}

I开发者_运维技巧 also want to execute this functionality when the page first loads, so I just add the following right after the above code:

$(window).resize();

This works just fine. However, I'm wondering if I may trigger any side effects, harmful or not, by calling this function - I really just want to execute my own resize handler. Of course, I could do the following to make sure that I execute only my handler:

var positionElements = function() {
    // Position elements
}
$(window).resize(positionElements);
positionElements();

However, I'm new to JavaScript and I want to keep my code as concise as possible - this adds some boiler plate code to the mix.

Edit: In fact, my code can be shortened even more by using chaining. Like this:

$(window).resize(function() {
    // Position elements
}).resize();


I cant see how it should be harmful, anything that could be triggered by a resize that is actually destructive should be avoided in the first place. What you are doing by using calling $(window).resize() is the same as the user resizing the window.

TL;DR; Yes its safe.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜