# Chrome viewport width/height detection problem?

**URL:** <https://forum.kirupa.com/t/chrome-viewport-width-height-detection-problem/328806>\
**Category:** web dev\
**Created:** [June 18, 2012, 6:06pm UTC](https://forum.kirupa.com/t/chrome-viewport-width-height-detection-problem/328806 "2012-06-18T18:06:48Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thovas](https://avatars.discourse-cdn.com/v4/letter/t/ba9def/32.png) [@Thovas](https://forum.kirupa.com/u/Thovas)\
**Post date:** [June 18, 2012, 6:06pm UTC](https://forum.kirupa.com/t/chrome-viewport-width-height-detection-problem/328806/1 "2012-06-18T18:06:48Z")

</div>

I have made small script that need to resize image to fit the entire stage of the browser  
code:

```auto

function resizeBg() {
            if ( (theWindow.width() / theWindow.height()) < aspectRatio ) {
                $bg
                    .removeClass()
                    .addClass('bgheight');
            } else {
                $bg
                    .removeClass()
                    .addClass('bgwidth');
            
            }
            var pW = (theWindow.width() - $bg.width())/2;
                        $bg.css("left", pW);
            var pH = (theWindow.height() - $bg.height())/2;
                        $bg.css("top", pH);
                        
        }
                                    
 theWindow.resize(function() {
       resizeBg();
  }).trigger("resize");

```

where $bg is reference to the image  
theWindow = $(window)  
and css classes .bgwidth and .bgheight have 100% width or height.

The code works as expected in FF and IE 7,8,9 (for my surprise) , but i have problems in Chrome.

theWindow.height() or theWindow.width() does not return anything, i have tryed some other window size detection functions but the result is pretty much the same.

**Edit: I manage to fix the problem. The calculations were trown off since Chrome fired up the event before the image is loaded. So i preloaded the image and that fixed the issue.**

Regards Thovas
