The Lodge is members-only design/dev videos and Office Hours.

Next Office Hours Session: "Implementing an SVG Icon System" Nov 30 - 6:00 PM Eastern

[Solved] $(document).width() giving wrong value on refresh in Chrome

  • # November 21, 2012 at 5:52 pm

    $(document).width() gives the proper width when I load the page. But when I refresh in Chome, it includes the width for the scrollbar and I’m not sure why. I’ve tried writing it a couple different ways but it keeps happening. Any ideas?

    # November 21, 2012 at 6:24 pm

    This reply has been reported for inappropriate content.

    I can’t replicate that. Do you have a test case for us?

    # November 21, 2012 at 6:27 pm

    Not at the current moment. Perhaps is has to do with a couple divs just inside the body with a width of 100%.

    # November 21, 2012 at 7:12 pm

    This reply has been reported for inappropriate content.

    I’m not sure if this is relevant, but does forcing the scroll bar to always show work (html { overflow-y: scroll; })?

    # November 21, 2012 at 7:51 pm

    Ooooooh, very good idea. That worked.

    I’m thinking what happened was the 100% elements was tricking my js to calculating the width before the scrollbars appear.

    This made me realize the real issue. I had forgot to wrap my js in $(document).ready() so it was grabbing it before it was loaded (and before the scrollbars existed)

    Thanks for the help

    EDIT: $(document).ready() didn’t stop the issue, so I guess I use overflow then. Unfavorable, but it’s okay.

    # November 21, 2012 at 8:02 pm

    This reply has been reported for inappropriate content.

    Yeah, I’m not really able to tell without being able to poke around the code, but at least that seems to be fixing it!

    # November 21, 2012 at 8:11 pm

    Interesting. Definitely keep it inside of doc.ready(), but maybe try using `$(document.body)` to calculate your width?

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.

There's a whole bunch of content on CSS-Tricks.

Search for Stuff   •   Browse the Archives

Get the Newsletter ... or get the RSS feed