What is the major difference between $(window).width()
vs $(document).width()
in jQuery?
Whether window denotes the browser and document represents the body of html page? Am I correct ?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- How to fix IE ClearType + jQuery opacity problem i
- void before promise syntax
- jQuery add and remove delay
$(window).width();
returns the width of browser viewport$(document).width();
returns width of HTML document$(document).width()
is a bit unreliable, resulting in a lower value for a full-screened browser . $(window).width() is safer.$(window).width()
gets the entire width of the window, including things like the scroll bar .From the documentation of
width()
:Simple jsFiddle Demo
In the demo, I have set
html { width: 1000px; }
, which is bigger than the viewport.The width of the body of your HTML page is a third value.
$('body').width()
can also differ from the other two (trybody { margin: 100px; }
for example).You are correct. the
window
is the viewable area of the browser. Thedocument
is the actually body of the page. So yourdocument
could extend far beyond thewindow
Another important difference.
$(window).width();
is available before the document loads / is ready$(document).width();
is only available after the document had loadedSo for the second, you need
Yes - width of window is width of browser window, and width of document is width of webpage.
So in conclusion the document could be smaller than the window.
FROM: http://eligeske.com/jquery/what-is-the-difference-between-document-and-window-objects-2/