Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
61
rated 0 times [  66] [ 5]  / answers: 1 / hits: 22402  / 11 Years ago, fri, october 4, 2013, 12:00:00

Ok, I thought this would be really simple, but it's turning out not to be. I think I'm just messing something up in my HTML/CSS, but here goes.



I have a basic page like so:



HTML



<!DOCTYPE html>
<html>
<head>
<link href='test2.css' rel=stylesheet type=text/css />
<script src=http://code.jquery.com/jquery-1.10.2.js></script>
<script src=test2.js></script>
</head>
<body>
<div id=scroll></div>
</body>
</html>


test2.css



* {
padding: 0;
margin: 0;
}

html, body {
height: 100%;
width: 100%;
}

#scroll {
height: 100%;
width: 100%;
overflow: scroll;
background-color: black;
}


test2.js



$(document).ready(function() {
// my resolution is 1440x900
alert('innerwidth should be 1425');
// all of these return 1440
alert('body innerwidth: ' + $('body').innerWidth());
alert('document width: ' + $(document).width());
alert('window width: ' + $(window).width());
alert('scroll div innerwidth: ' + $('#scroll').innerWidth());
alert('document.documentElement.clientWidth: ' + document.documentElement.clientWidth);
alert('document.documentElement.scrollWidth: ' + document.documentElement.scrollWidth);
});


So I've got one element on the page... a div that takes up the entire screen, or rather it should be taking up the entire screen minus the scrollbars. Now, I've been doing some snooping on how to grab the width and height of a page without the scrollbars, but unfortunately, none of them return the proper value... which makes me believe I'm missing the boat in my HTML or CSS.



I looked at the following:



jquery - how to get screen width without scrollbar?



how to get the browser window size without the scroll bars



So what I need is for a method to return the value of my viewable screen minus the respective scrollbar value... so for my width, my value should be 1425 because the scrollbar is 15 pixels wide. I thought that's what innerWidth's job was, but apparently I'm wrong?



Can anyone provide any insight? (I'm running Firefox 24.)



EDIT



To add some background, I've got a blank page. I will be adding elements one by one to this page, and I need to use the width of the page when calculating the sizes for these elements. Eventually, this page will grow and grow until the scrollbar appears, which is why I'm trying to force the scrollbar there from the start, but apparently, that still doesn't do anything.



EDIT2



Here's something even more interesting... if I do document.getElementById('scroll').clientWidth, I get the proper innerWidth, but if I do $('#scroll').width() or $('#scroll').innerWidth(), they both return the max resolution... sounds like a jQuery bug.


More From » html

 Answers
28

Discovered a very hacky solution... by adding this before my alerts in test2.js, I get the proper width:



var p = $('body').append('<p style=height: 100%; width: 100%;></p>');
alert(p.width());
$('body').remove('p');


And consequently, all of the alerts now have the proper width. I also don't even need overflow-y in the CSS if I do it this way. Curious why this solves it...



The real answer should be keeping the HTML and CSS as is, then using document.getElementById('scroll').clientWidth. Using clientWidth gets the viewable area minus the scrollbar width.


[#75237] Thursday, October 3, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ravenl

Total Points: 338
Total Questions: 107
Total Answers: 112

Location: Belize
Member since Mon, Jun 20, 2022
2 Years ago
ravenl questions
Thu, Feb 18, 21, 00:00, 3 Years ago
Tue, Jan 12, 21, 00:00, 3 Years ago
Tue, Mar 17, 20, 00:00, 4 Years ago
;