- This topic is empty.
Viewing 6 posts - 1 through 6 (of 6 total)
Viewing 6 posts - 1 through 6 (of 6 total)
- The forum ‘JavaScript’ is closed to new topics and replies.
The forums ran from 2008-2020 and are now closed and viewable here as an archive.
Home › Forums › JavaScript › Script randomly not running
I have a jquery script that centers my logo on the middle of the screen. It is set up to run on page load and window resize. It works every time on window resize, and 9 times out of 10 on load. But once in a while it completely fails to run on load.
Here’s my [http://vividconference.tv/beta/](http://vividconference.tv/beta/ “Website”)
Thanks!
And the script in question:
$(window).resize(function(){
var height = $(‘#logo’).height();
$(‘#logo’).css(‘margin-top’, + height / -2 + ‘px’);
}).resize();
that code is running on resize but not on page load.
the time this code is executed it hasnt a reference in #logo ,on resize it has because the html has been parsed,
enclose your code in a $(your code) is the same as writing
$(document).ready()
$(function(){
$(window).resize(function(){
var height = $(‘#logo’).height();
$(‘#logo’).css(‘margin-top’, + height / -2 + ‘px’);
}).resize();
})
Thanks for the suggestion! It works fine the vast majority of time, but once in a while it still fails to work. Any more suggestions?
Try to wait like 10ms before calling `resize()`
setTimeout(function() {
$(window).resize();
}, 10);
Is `#logo` an image? If so, you’ll need to wait for the image to fully load before you can calculate its height.