jQuery width() function returns wrong value in Internet Explorer 9

12,587

Solution 1

Try the outerWidth, and make sure to wrap it in a windows.ready event listener to make sure all DOM element rendered properly before the width being computed

$("#slider3").outerWidth()

Solution 2

I've had problems with jQuery's width()/height()/offset().top/.left etc in the past when I used them before a certain event fully bubbled. See if setTimeout(function() { alert($('#slider').width()); }, 0); has any effect. It's a cheap nextTick() trick that might be just what you need.

Share:
12,587
HP.
Author by

HP.

Updated on June 26, 2022

Comments

  • HP.
    HP. almost 2 years

    I used Easy Slider jQuery plugin before and never had problem until now. And the problem is strange. Check out this home page http://bit.ly/HKiWY6

    The page will pop an alert showing two values:

    $("#slider").width()
    

    and

    $("#slider3").width()
    

    I already set the value for both in css. One is 710px and one is 700px.

    If you run in IE9, it shows the default value of $(window).width() instead for both, whatever the window or document width currently is. FF and Chrome returned correctly. Why is that?

  • HP.
    HP. about 12 years
    Thanks. That works but I ended up fixing the slider.js of Easy Slider 1.7. Not that I want to mess with the core code but look like that's the only way. I was kinda hoping to fix it from within my HTML/CSS. Because this example url works fine cssglobe.com/lab/easyslider1.7/02.html
  • Phradion
    Phradion about 12 years
    well.. I'm confused, so it works for u now but you still have to modify the lib, or are you saying that the .width computation is part of the lib? Have you tried wrapping your code within $(document).ready(function(){ } ?
  • HP.
    HP. about 12 years
    The .width is part of the lib. So I guess the lib has the bug in IE9 for certain html/javascript running. I was trying to figure out if I can change things in my current template of html/js instead...