davids
davids

Reputation: 6371

Javascript - Getting document width

I'd like to get the document's width with javascript (jQuery available), which I could achieve simply with document.width. The problem comes up when the contents are positioned with a negative value, because document.width will retrieve the width less that value (you can see this behavior in http://jsfiddle.net/McgzK/1/). So, is there a way I can get the document's width straight forward, or should I do something like document.width - parseInt($(container).css('left'))?

Upvotes: 1

Views: 609

Answers (2)

Shiplu Mokaddim
Shiplu Mokaddim

Reputation: 57690

There is no straight forward way to get that width. This width does not make any sense. document.width is the value of documents width. If you put something outside document layout its not included in documents width as its not in the document. So for your Total width you have to add that portion.

Use the scrollWidth property of your element and add absolute left value of css position.

See http://jsfiddle.net/McgzK/11/

Note:

  1. if you want the width of content use scrollWidth property.
  2. If you want to get the width of the element use offsetWidth.

Upvotes: 1

Alan Shortis
Alan Shortis

Reputation: 1109

Try setting your body width to 100% in your CSS, then use jQuery to get the width of the body rather than the document.

Upvotes: 0

Related Questions