CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript frameworks (http://www.codingforums.com/forumdisplay.php?f=62)
-   -   waiting for a window.load value (http://www.codingforums.com/showthread.php?t=289532)

bolo77 03-09-2013 04:14 PM

waiting for a window.load value
 
Hi,

im attempting to read the height value of an element which doesnt have its height specified.

If i get its value within document.ready then it intermittently returns the wrong value as im guessing the element has yet to be populated.

I can get the correct value when using window.load but then need to pass this value to my code within document.ready. Is this possible? How else could i solve this. moving everything within a window.load doesnt work.

Code:


                                $(document).ready(function() {
                            clearTimeout( timeout );
                                  timeout = setTimeout( function() {
                               
                                         
                                applyResult(loadMaindata, function() {

                                var imgWidth = $('.' + divID +'_col').width();
                                var imgHeight = $('.' + divID +'_col').height();

                               
                                console.log("image height "+ imgHeight)  // doesnt always get the correct value
                       
                                            /* initiate plugin */
                                                    $('.' + divID +'_pag').jPages({
                                                containerID : divID +"_gallery",
                                                perPage : rows,

                                                                callback    : function( pages, items ){
                                                                    /* lazy load current images */
                                                                        items.showing.find("img").lazyload({event:"turnPage",effect:"fadeIn"}).trigger("turnPage");
                                                                        console.log("items "+ items.count)
                                                                        console.log("rows "+ rows)
                                                                        console.log("pages "+ pages.count)

                                                                        if (pages.count >1)
                                                                          {
                                                                          }
                                                                            $('.' + divID +'_pag').css('display', 'block')       
                                                                                       
                                                                }
                                                    },$('.' + divID +'_gallery li').css('display', 'list-item'));


                                });

                               
                                        }, 500 );

       



                                  },function(){

                                        clearTimeout( timeout );
                                       
                         
                                        });


bolo77 03-09-2013 06:57 PM

simplified example
 
Code:

        $(window).load(function() {
                                        var imgHeight1 = $('.' + divID +'_col').height();
                                        console.log(imgHeight1) 
// correct height is returned.  How do i send this to the document.ready function below
                                });
                       
                                $(document).ready(function() {
                                var imgHeight2 = $('.' + divID +'_col').height();
// this returns the wrong height. can i get the height value from the .load  function and wait until i have this before processing anything else in the doc.ready function?
                                });


felgall 03-09-2013 09:07 PM

Put your script at the bottom of the page and get rid of the unnecessary ready/load wrapper that is slowing down the loading of the page.

That might not fix the problem but at least you'll have your JavaScript where it belongs and will be able to tell if there are additional things it needs other than the entire HTML in the page before the script can run - and if it doesn't work properly there because of not everything it needs being loaded yet then you will know that it has to wait for the load event to be triggered.

bolo77 03-10-2013 12:21 PM

solved
 
i solved the problem by loading the first li tag with its content so i could get its height value and append it to the other li's tags. Next i hide the first li again and then continue with my normal load

Now the browser always gets the correct height value for the li tags :)


All times are GMT +1. The time now is 12:54 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.