473,545 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

node.offsetWidt h vs clientWidth vs scrollWidth


for the code

var node = document.getEle mentById("somet hing")
alert(node.offs etWidth)

is very similar to node.clientWidt h and node.scrollWidt h

I just wonder why offsetWidth is well documented in the Definitive
Javascript book, but clientWidth and scrollWidth are not mentioned at
all, even when all 3 of them work in IE6, IE 7, Firefox 2, and Safari
3.

Also it seems that scrollWidth is always the same as offsetWidth. I
can't make them different by hiding some part of that element so that
one value is smaller and one is larger.

Thanks very much!

Jun 27 '08 #1
5 21894
On Thu, 24 Apr 2008 11:17:42 +0200, liketofindoutwh y
<li************ **@gmail.comwro te:
>
for the code

var node = document.getEle mentById("somet hing")
alert(node.offs etWidth)

is very similar to node.clientWidt h and node.scrollWidt h

I just wonder why offsetWidth is well documented in the Definitive
Javascript book, but clientWidth and scrollWidth are not mentioned at
all, even when all 3 of them work in IE6, IE 7, Firefox 2, and Safari
3.

Also it seems that scrollWidth is always the same as offsetWidth. I
can't make them different by hiding some part of that element so that
one value is smaller and one is larger.
I have no intimate knowledge of the subject, but making them all 3
different is no trouble at all:
<html>
<head><title>te st</title>
<script type="text/javascript">
function test(elm){
alert('offsetwi dth:' + elm.offsetWidth + ', scrollWidth: '
+ elm.scrollWidth + ', clientWidth: ' + elm.clientWidth );
}
</script>
</head>
<body>
<div onclick="test(t his)"
style="display: block;width:8em ;height:3em;ove rflow:scroll;">
aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaa<br>
aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaa<br>
aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaa<br>
aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaa<br>
aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaa<br>
aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaa<br>
aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaa<br>
aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaa<br>
aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaa<br>
</div>
</body>
</html>
Results (FF2)here: 128, 477,112; I suspect, but feel free to correct me:

offsetWidth: width on page
scrollWidth: total width of element from left to right (so in this case
scrollable content).
clientWidth: visible portion of width due to existance of scrollbar.
--
Rik Wasmus
Jun 27 '08 #2
liketofindoutwh y <li************ **@gmail.comwri tes:
for the code

var node = document.getEle mentById("somet hing")
alert(node.offs etWidth)

is very similar to node.clientWidt h and node.scrollWidt h

I just wonder why offsetWidth is well documented in the Definitive
Javascript book, but clientWidth and scrollWidth are not mentioned at
all, even when all 3 of them work in IE6, IE 7, Firefox 2, and Safari
3.
Last time I checked, only the offset* attributes were reliable in all
popular browsers. This may have changed, though. See

http://www.quirksmode.org/viewport/e...imensions.html
Also it seems that scrollWidth is always the same as offsetWidth. I
can't make them different by hiding some part of that element so that
one value is smaller and one is larger.
Scrollwidth isn't about hiding per se, it's about having a content area
wich is scrolled inside an area that's smaller. IOW, it's using
overflow: auto / srcoll.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Jun 27 '08 #3
On Apr 24, 2:35 am, "Rik Wasmus" <luiheidsgoe... @hotmail.comwro te:
I have no intimate knowledge of the subject, but making them all 3
different is no trouble at all:

if it is

<html>
<head><title>te st</title>
<script type="text/javascript">
function test(elm){
alert('offsetwi dth:' + elm.offsetWidth + ',
scrollWidth: '
+ elm.scrollWidth + ', clientWidth: ' + elm.clientWidth );
}
</script>
</head>
<body>
<div style="display: block;width:8em ;height:
3em;overflow:sc roll;">
<img onclick="test(t his)" src="http://
www.sanrio.co.j p/english/characters/w_chara/pocha180.gif"
</div>
</body>
</html>

then the offsetWidth and scrollWidth is the same... so it looks like
scrollWidth reflects an element with a scroll box, not an element
contained in a scrollbox.

Jun 27 '08 #4
On Apr 24, 3:22 am, liketofindoutwh y <liketofindout. ..@gmail.com>
wrote:
On Apr 24, 2:35 am, "Rik Wasmus" <luiheidsgoe... @hotmail.comwro te:

then the offsetWidth and scrollWidth is the same... so it looks like
scrollWidth reflects an element with a scroll box, not an element
contained in a scrollbox.
i mean reflects something different for an element with a scrollbox,
nothing different for an element simply being inside the scrollbox.

Jun 27 '08 #5
On Thu, 24 Apr 2008 12:25:27 +0200, liketofindoutwh y
<li************ **@gmail.comwro te:
On Apr 24, 3:22 am, liketofindoutwh y <liketofindout. ..@gmail.com>
wrote:
>On Apr 24, 2:35 am, "Rik Wasmus" <luiheidsgoe... @hotmail.comwro te:

then the offsetWidth and scrollWidth is the same... so it looks like
scrollWidth reflects an element with a scroll box, not an element
contained in a scrollbox.

i mean reflects something different for an element with a scrollbox,
nothing different for an element simply being inside the scrollbox.
I'd say that would be so, and for good reason. The width of the image
element is not altered in any way, it just happens to be in a element
which itself is not 'fully extended'.
--
Rik Wasmus
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
7235
by: BT | last post by:
Hi. I like to know where and how do you set the ClientHeight and ClientWidth properties? I can see Height,Width,Top,Left in the Properties window. Any suggestion would be very helpful. Thank you, -BT
1
6173
by: Robert Oschler | last post by:
I have a "child" document that I use for an IFRAME element that I put into several "parent" documents. These "parent" documents therefore contain the IFRAME whose SRC property is set to the "child" document. The parent(s) have three Javascript functions, here they are: // ============================= var gMargin = 5;
1
2980
by: parksch2 | last post by:
I know there are a lot of articles out there about this subject but I have been reading for 3 hours and still can't find the answer. I have some text in a bunch of hidden spans. I need to find the width in pixels of that text. Using offsetWidth gives me a different value when viewed in IE and Firefox. I need those to sync up somehow. I have...
3
4677
by: mscir | last post by:
I'm adding text to a div using innerHTML, and watching the width of the div using offsetWidth. In IE the offsetWidth increases when the div gets wider, but in Netscape 7.2 or Firefox 1.0.3 it doesn't. Is there a way I can find when a div increases its width in non-IE browsers? TIA, Mike
16
13319
by: Joel Byrd | last post by:
I am having this ridiculous problem of trying to set the innerHTML of a div node. It works in all other browsers, but internet explorer is giving me an "Unknown runtime error". This is actually in the context of developing an auto-suggest (basically reverse-engineering Google Suggest), but I don't see how any of the other code has anything...
3
11312
by: jdlwright | last post by:
Hi, I need to place a DIV exactly over a textarea element, but I can't calculate the correct value that I should set style.width to, because offsetWidth and style.width are calculated differently under strict mode. Under strict mode, offsetWidth returns the width of an element plus it's padding and border (i.e. just the same as quirks...
2
10985
by: Noah Sussman | last post by:
Hello, I am writing a function to reposition an element based on whether one of its edges is outside the visible area of the browser window (the code is below). My client has asked for code that runs in IE6 and Firefox. However, I am having a problem in Firefox. I've recently discovered that Firefox considers the clientWidth and...
1
10777
by: Beni Rose | last post by:
I'm trying to build some dynamic vertical drop down menus (pop out menus?) and I'm having some trouble. I'm trying to get the width of the span tag that the root level menu item is in, because it's going to be different every time (it's a solution for a template driven website solution). I'm not assigning the width in my javascript for the...
0
1327
by: rodchar | last post by:
hey all, i have the following DIV element in my page: <DIV id="OnlyDIV" style="OVERFLOW: hidden; WIDTH: 300px">... This div element contains a datalist. This works fine for me on a regular webform in that it gives me the correct scrollWidth value. The problem is when i place this code inside my master page the scrollWidth value then...
0
7393
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7653
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7803
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7411
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5322
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3444
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3439
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1012
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
695
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.