Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between $(window).width() vs $(document).width()

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 ?

like image 839
kbvishnu Avatar asked Feb 24 '12 12:02

kbvishnu


People also ask

Is viewport the same as window?

A viewport represents the area in computer graphics being currently viewed. In web browser terms, it is generally the same as the browser window, excluding the UI, menu bar, etc. That is the part of the document you are viewing.


1 Answers

From the documentation of width():

This method is also able to find the width of the window and document.

$(window).width();   // returns width of browser viewport $(document).width(); // returns width of HTML document 

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 (try body { margin: 100px; } for example).

like image 162
kapa Avatar answered Sep 22 '22 14:09

kapa