treehouse : what would you like to learn today?
Web Design Web Development iOS Development

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

  • $(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?

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

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

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

  • 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.

  • 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!

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