Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery: (window).resize works, (document).ready doesn't

Tags:

jquery

I am having some issues getting this JQuery script to work. It works flawlessly when resizing the image, but I can't get this to work on the initial page load. Any ideas? I'm completely stuck and nothing is working for me.

   <script type="text/javascript">
        $(window).resize(function () {
            if ($(window).width() <= 600) {
                $('#fcalendar').fullCalendar('changeView', 'basicDay');
            } else if ($(window).width() < 748) {
                $('#fcalendar').fullCalendar('changeView', 'basicDay');
            } else {
                $('#fcalendar').fullCalendar('changeView', 'month');
            }
        });

        $(document).ready(function () {
            if ($(document).width() <= 600) {
                $('#fcalendar').fullCalendar('changeView', 'basicDay');
            } else if ($(window).width() < 748) {
                $('#fcalendar').fullCalendar('changeView', 'basicDay');
            } else {
                $('#fcalendar').fullCalendar('changeView', 'month');
            }
        });
    </script>
like image 214
Michael Stenberg Avatar asked Mar 15 '26 18:03

Michael Stenberg


1 Answers

As indicated in comments, document.ready != window.onload. Document.ready will not wait for the images to actually load. Thus, it can give bad results. You should hook into the window.onload event when you want to wait for images to fully finish (this is also the mechanism used in parallax sites which need to wait for images to load).

window.onload = function () {
        if ($(document).width() <= 600) {
            $('#fcalendar').fullCalendar('changeView', 'basicDay');
        } else if ($(window).width() < 748) {
            $('#fcalendar').fullCalendar('changeView', 'basicDay');
        } else {
            $('#fcalendar').fullCalendar('changeView', 'month');
        }
};
like image 170
Travis J Avatar answered Mar 18 '26 08:03

Travis J



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!