Reputation: 1456
I want to call a JQuery function on window resize and also during the initial load. I just tried this, I am sure there is a better way, Please explain this learner,
$(document).ready(function () {
$(function () {
var doosomething = function () {
$('#bottomDiv').css('top', $(window).height() - 105);
}
$(window).resize(doosomething);
});
});
Upvotes: 1
Views: 138
Reputation: 2053
You may find window.resize to be unreliable at times. Different browsers call that at different times. (For instance, do you want it to happen "live" or after they stop resizing?)
I would suggest using:
setInterval(doosomething, 500);
Or something like that. Your code is fine though, outside of your unnecessary nesting. And not doing an initial call before the resize (if that is what you want to do).
Upvotes: 0
Reputation: 322592
No need to name the function just so you can call it.
You can do it like this:
$(function () {
// -----------v-----------assign the handler
$(window).resize(function () {
$('#bottomDiv').css('top', $(window).height() - 105);
}).resize();
});
// ---^--- invoke the handler
Upvotes: 3
Reputation: 222398
Try
$(document).ready(function () {
var do_something = function () {
$('#bottomDiv').css('top', $(window).height() - 105);
}
$(window).resize(do_something);
do_something();
});
Your call to $(function () {
isn't of any use, as it does exactly the same thing as $(document).ready(function () {
.
Upvotes: 4