Get the size of the screen, current web page and browser window
How can I get windowWidth, windowHeight, pageWidth, pageHeight, screenWidth, screenHeight, pageX, pageY, screenX, screenY which will work in all major browsers?

javascript jquery layout cross-browser
|
show 2 more comments
How can I get windowWidth, windowHeight, pageWidth, pageHeight, screenWidth, screenHeight, pageX, pageY, screenX, screenY which will work in all major browsers?

javascript jquery layout cross-browser
520
Nice pic. All questions should and answers should be like this.
– Damien Golding
Jun 12 '13 at 8:57
8
pageHeight(on a pic) u can get with: document.body.scrollHeight
– befzz
Dec 11 '13 at 20:29
3
see https://developer.mozilla.org/en-US/docs/Web/API/Window.screen and http://www.quirksmode.org/dom/w3c_cssom.html#screenview
– rawiro
May 26 '14 at 23:06
3
Interesting: ryanve.com/lab/dimensions
– Ring Ø
Jul 31 '14 at 9:59
1
Helpful tutorial -- w3schools.com/jsref/prop_win_innerheight.asp
– Uncle Iroh
Aug 1 '16 at 12:55
|
show 2 more comments
How can I get windowWidth, windowHeight, pageWidth, pageHeight, screenWidth, screenHeight, pageX, pageY, screenX, screenY which will work in all major browsers?

javascript jquery layout cross-browser
How can I get windowWidth, windowHeight, pageWidth, pageHeight, screenWidth, screenHeight, pageX, pageY, screenX, screenY which will work in all major browsers?

javascript jquery layout cross-browser
javascript jquery layout cross-browser
edited Mar 29 '17 at 20:38
GISKid
2691318
2691318
asked Aug 9 '10 at 6:28
turtledoveturtledove
9,97931619
9,97931619
520
Nice pic. All questions should and answers should be like this.
– Damien Golding
Jun 12 '13 at 8:57
8
pageHeight(on a pic) u can get with: document.body.scrollHeight
– befzz
Dec 11 '13 at 20:29
3
see https://developer.mozilla.org/en-US/docs/Web/API/Window.screen and http://www.quirksmode.org/dom/w3c_cssom.html#screenview
– rawiro
May 26 '14 at 23:06
3
Interesting: ryanve.com/lab/dimensions
– Ring Ø
Jul 31 '14 at 9:59
1
Helpful tutorial -- w3schools.com/jsref/prop_win_innerheight.asp
– Uncle Iroh
Aug 1 '16 at 12:55
|
show 2 more comments
520
Nice pic. All questions should and answers should be like this.
– Damien Golding
Jun 12 '13 at 8:57
8
pageHeight(on a pic) u can get with: document.body.scrollHeight
– befzz
Dec 11 '13 at 20:29
3
see https://developer.mozilla.org/en-US/docs/Web/API/Window.screen and http://www.quirksmode.org/dom/w3c_cssom.html#screenview
– rawiro
May 26 '14 at 23:06
3
Interesting: ryanve.com/lab/dimensions
– Ring Ø
Jul 31 '14 at 9:59
1
Helpful tutorial -- w3schools.com/jsref/prop_win_innerheight.asp
– Uncle Iroh
Aug 1 '16 at 12:55
520
520
Nice pic. All questions should and answers should be like this.
– Damien Golding
Jun 12 '13 at 8:57
Nice pic. All questions should and answers should be like this.
– Damien Golding
Jun 12 '13 at 8:57
8
8
pageHeight(on a pic) u can get with: document.body.scrollHeight
– befzz
Dec 11 '13 at 20:29
pageHeight(on a pic) u can get with: document.body.scrollHeight
– befzz
Dec 11 '13 at 20:29
3
3
see https://developer.mozilla.org/en-US/docs/Web/API/Window.screen and http://www.quirksmode.org/dom/w3c_cssom.html#screenview
– rawiro
May 26 '14 at 23:06
see https://developer.mozilla.org/en-US/docs/Web/API/Window.screen and http://www.quirksmode.org/dom/w3c_cssom.html#screenview
– rawiro
May 26 '14 at 23:06
3
3
Interesting: ryanve.com/lab/dimensions
– Ring Ø
Jul 31 '14 at 9:59
Interesting: ryanve.com/lab/dimensions
– Ring Ø
Jul 31 '14 at 9:59
1
1
Helpful tutorial -- w3schools.com/jsref/prop_win_innerheight.asp
– Uncle Iroh
Aug 1 '16 at 12:55
Helpful tutorial -- w3schools.com/jsref/prop_win_innerheight.asp
– Uncle Iroh
Aug 1 '16 at 12:55
|
show 2 more comments
16 Answers
16
active
oldest
votes
If you are using jQuery, you can get the size of the window or document using jQuery methods:
$(window).height(); // returns height of browser viewport
$(document).height(); // returns height of HTML document (same as pageHeight in screenshot)
$(window).width(); // returns width of browser viewport
$(document).width(); // returns width of HTML document (same as pageWidth in screenshot)
For screen size you can use the screen object in the following way:
screen.height;
screen.width;
1
thanks, and is there any way to get pageX, pageY, screenX, screenY?
– turtledove
Aug 10 '10 at 2:25
2
The jQuery method height() seems to work for all elements, and returns a number (46) rather than a string like css('height') ("46px").
– Chris
Feb 6 '13 at 16:02
6
When dealing with mobile Safari, sadly jQuery isn't a perfect solution to this question. See the note on line #13 at github.com/jquery/jquery/blob/master/src/dimensions.js
– Joshua
Jun 19 '13 at 17:10
8
@웃웃웃웃웃 what did you edit in the answer? according to the revisions, you didn't edit anything at all
– DanFromGermany
Jan 28 '14 at 15:31
8
@Marco Kerwitz The worst thing is that I typed "javascript get window width" and the content of this answer was on Google. A big minus one from me.
– Maciej Krawczyk
Jun 11 '16 at 7:43
|
show 16 more comments
This has everything you need to know: Get viewport/window size
but in short:
var w = window,
d = document,
e = d.documentElement,
g = d.getElementsByTagName('body')[0],
x = w.innerWidth || e.clientWidth || g.clientWidth,
y = w.innerHeight|| e.clientHeight|| g.clientHeight;
alert(x + ' × ' + y);
Fiddle
53
Why notg = document.body?
– a paid nerd
Jan 27 '14 at 3:32
50
@apaidnerd: Standards defying browsers like IE8 do not support document.body. IE9, however, does.
– Michael Mikowski
Jan 27 '14 at 22:34
42
@MichaelMikowski That is not true! Even IE5 supportsdocument.body.
– Nux
Sep 27 '14 at 19:48
30
@nux I stand corrected, and I've confirmed support in IE8. I know though that at least one brower we were targeting recently did not support document.body and we had to change to use the getElementsByTagName approach. But I guess I misremembered the browser. Sorry!
– Michael Mikowski
Sep 28 '14 at 1:07
16
I would just like to very quierly remark that one-letter variable names are never helpful.
– wybe
Jun 2 '18 at 23:27
|
show 7 more comments
Here is a cross browser solution with pure JavaScript (Source):
var width = window.innerWidth
|| document.documentElement.clientWidth
|| document.body.clientWidth;
var height = window.innerHeight
|| document.documentElement.clientHeight
|| document.body.clientHeight;
9
This is better because if you are able to call the script early enough in the loading process (often the idea), then thebody elementwill return a value ofundefinedas the dom isn't loaded yet.
– dgo
Aug 14 '15 at 19:00
11
HA! Old thread but thanks for that! I guess I'm one of those old "idiots" that tries to support at least back to IE8 when possible, for the benefit of the surprising number of older home users who will never stop using XP until their machines catch fire. I get tired of asking questions and instead of getting an answer, getting down-voted with only "STOP SUPPORTING IE8!!" as a comment. Again thanks! This solved a problem for me in a pure javascript photo zoom I had done. Its a little slow on IE8, but now at least it works!!! :-)
– Randy
Sep 15 '16 at 20:32
great it is a lot better than rest.
– vinayak_shahdeo
15 hours ago
add a comment |
A non-jQuery way to get the available screen dimension. window.screen.width/height has already been put up, but for responsive webdesign and completeness sake I think its worth to mention those attributes:
alert(window.screen.availWidth);
alert(window.screen.availHeight);
http://www.quirksmode.org/dom/w3c_cssom.html#t10 :
availWidth and availHeight - The available width and height on the
screen (excluding OS taskbars and such).
same as width screen.height - on android chrome browser shows divided by pixel ratio :(
– Darius.V
Jan 30 '15 at 9:52
2
window.screen.availHeightseems to assume full screen mode so that the normal screen mode forces scrolling (tested in Firefox and Chrome).
– Suzana
Mar 21 '15 at 15:13
@Suzana_K then usewindow.screen.heightinstead.
– Vallentin
Apr 2 '16 at 1:40
add a comment |
But when we talk about responsive screens and if we want to handle it using jQuery for some reason,
window.innerWidth, window.innerHeight
gives the correct measurement. Even it removes the scroll-bar's extra space and we don't need to worry about adjusting that space :)
2
This should be the accepted answer with loads of upvotes. A much more useful answer than the currently accepted answer, and it also doesn't depend on jQuery.
– developerbmw
Apr 29 '15 at 23:25
4
Unfortunately,window.innerWidthandwindow.innerHeightfail to take the size of the scroll bar into account. If scroll bars are present, these methods return wrong results for the window size in nearly all desktop browsers. See stackoverflow.com/a/31655549/508355
– hashchange
Aug 25 '15 at 11:13
add a comment |
To check height and width of your current loaded page of any website using "console" or after clicking "Inspect".
step 1: Click the right button of mouse and click on 'Inspect' and then click 'console'
step 2: Make sure that your browser screen should be not in 'maximize' mode. If the browser screen is in 'maximize' mode, you need to first click the maximize button (present either at right or left top corner) and un-maximize it.
step 3: Now, write the following after the greater than sign ('>') i.e.
> window.innerWidth
output : your present window width in px (say 749)
> window.innerHeight
output : your present window height in px (say 359)
1
Why isn't this the selected answer?
– Iulian Onofrei
Oct 8 '16 at 18:30
1
@IulianOnofrei because it does not fully answer the original set of questions. Joke being, it is the correct answer for me.
– rob
Jan 18 '17 at 15:06
innerWidth and innerHeight are only accurate if the viewport scale is 1. This is the default, but it can be inadvertently changed using css transforms.
– Victor Stoddard
Mar 14 '18 at 0:49
"window.innerWidth" as opposed to "screen.width" worked for me to determine size of user's browser - thank you!
– Kevin Pajak
Jul 24 '18 at 11:02
add a comment |
You can also get the WINDOW width and height, avoiding browser toolbars and other stuff. It is the real usable area in browser's window.
To do this, use:
window.innerWidth and window.innerHeight properties (see doc at w3schools).
In most cases it will be the best way, in example, to display a perfectly centred floating modal dialog. It allows you to calculate positions on window, no matter which resolution orientation or window size is using the browser.
2
This is not supported in ie8 nor ie7, according to w3schools. Also, you must watch out when you link to w3schools. See meta.stackexchange.com/questions/87678/…
– thecarpy
Feb 4 '15 at 6:31
add a comment |
function wndsize(){
var w = 0;var h = 0;
//IE
if(!window.innerWidth){
if(!(document.documentElement.clientWidth == 0)){
//strict mode
w = document.documentElement.clientWidth;h = document.documentElement.clientHeight;
} else{
//quirks mode
w = document.body.clientWidth;h = document.body.clientHeight;
}
} else {
//w3c
w = window.innerWidth;h = window.innerHeight;
}
return {width:w,height:h};
}
function wndcent(){
var hWnd = (arguments[0] != null) ? arguments[0] : {width:0,height:0};
var _x = 0;var _y = 0;var offsetX = 0;var offsetY = 0;
//IE
if(!window.pageYOffset){
//strict mode
if(!(document.documentElement.scrollTop == 0)){offsetY = document.documentElement.scrollTop;offsetX = document.documentElement.scrollLeft;}
//quirks mode
else{offsetY = document.body.scrollTop;offsetX = document.body.scrollLeft;}}
//w3c
else{offsetX = window.pageXOffset;offsetY = window.pageYOffset;}_x = ((wndsize().width-hWnd.width)/2)+offsetX;_y = ((wndsize().height-hWnd.height)/2)+offsetY;
return{x:_x,y:_y};
}
var center = wndcent({width:350,height:350});
document.write(center.x+';<br>');
document.write(center.y+';<br>');
document.write('<DIV align="center" id="rich_ad" style="Z-INDEX: 10; left:'+center.x+'px;WIDTH: 350px; POSITION: absolute; TOP: '+center.y+'px; HEIGHT: 350px"><!--К сожалению, у Вас не установлен flash плеер.--></div>');
add a comment |
If you need a truly bulletproof solution for the document width and height (the pageWidth and pageHeight in the picture), you might want to consider using a plugin of mine, jQuery.documentSize.
It has just one purpose: to always return the correct document size, even in scenarios when jQuery and other methods fail. Despite its name, you don't necessarily have to use jQuery – it is written in vanilla Javascript and works without jQuery, too.
Usage:
var w = $.documentWidth(),
h = $.documentHeight();
for the global document. For other documents, e.g. in an embedded iframe you have access to, pass the document as a parameter:
var w = $.documentWidth( myIframe.contentDocument ),
h = $.documentHeight( myIframe.contentDocument );
Update: now for window dimensions, too
Ever since version 1.1.0, jQuery.documentSize also handles window dimensions.
That is necessary because
$( window ).height()is buggy in iOS, to the point of being useless
$( window ).width()and$( window ).height()are unreliable on mobile because they don't handle the effects of mobile zooming.
jQuery.documentSize provides $.windowWidth() and $.windowHeight(), which solve these issues. For more, please check out the documentation.
VM2859:1 Uncaught DOMException: Blocked a frame with origin "localhost:12999" from accessing a cross-origin frame.(…) is there any way to over come this ???
– fizmhd
Jul 13 '16 at 10:44
1
You can't access iframes from a different domain, it violates the same-origin policy. Check out this answer.
– hashchange
Jul 13 '16 at 10:53
This not working if you are embedding an iframe and trying to get the height of the view port. window.innerHeight equal to the document height which is buggy. You cannot access frames. from a different domain it's not a feasible solution.
– M.Abulsoud
Dec 12 '18 at 13:47
@M.Abulsoud Provided you have access to the iframe (no CORS violation), it does work and is covered by the test suite. Use this syntax. It even works with multiple iframes nested inside each other - see this example on Codepen. If your setup works generally, but fails in a single specific browser, it might be a bug I'm not aware of. Perhaps you can open an issue in that case? Thanks.
– hashchange
Dec 12 '18 at 16:24
add a comment |
I wrote a small javascript bookmarklet you can use to display the size. You can easily add it to your browser and whenever you click it you will see the size in the right corner of your browser window.
Here you find information how to use a bookmarklet
https://en.wikipedia.org/wiki/Bookmarklet
Bookmarklet
javascript:(function(){!function(){var i,n,e;return n=function(){var n,e,t;return t="background-color:azure; padding:1rem; position:fixed; right: 0; z-index:9999; font-size: 1.2rem;",n=i('<div style="'+t+'"></div>'),e=function(){return'<p style="margin:0;">width: '+i(window).width()+" height: "+i(window).height()+"</p>"},n.html(e()),i("body").prepend(n),i(window).resize(function(){n.html(e())})},(i=window.jQuery)?(i=window.jQuery,n()):(e=document.createElement("script"),e.src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js",e.onload=n,document.body.appendChild(e))}()}).call(this);
Original Code
The original code is in coffee:
(->
addWindowSize = ()->
style = 'background-color:azure; padding:1rem; position:fixed; right: 0; z-index:9999; font-size: 1.2rem;'
$windowSize = $('<div style="' + style + '"></div>')
getWindowSize = ->
'<p style="margin:0;">width: ' + $(window).width() + ' height: ' + $(window).height() + '</p>'
$windowSize.html getWindowSize()
$('body').prepend $windowSize
$(window).resize ->
$windowSize.html getWindowSize()
return
if !($ = window.jQuery)
# typeof jQuery=='undefined' works too
script = document.createElement('script')
script.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js'
script.onload = addWindowSize
document.body.appendChild script
else
$ = window.jQuery
addWindowSize()
)()
Basically the code is prepending a small div which updates when you resize your window.
add a comment |
In some cases related with responsive layout $(document).height() can return wrong data that displays view port height only.
For example when some div#wrapper has height:100%, that #wrapper can be stretched by some block inside it. But it's height still will be like viewport height. In such situation you might use
$('#wrapper').get(0).scrollHeight
That represents actual size of wrapper.
So many answer with hundreds of upvotes but only this one was useful.
– Nakilon
Feb 12 '18 at 8:06
add a comment |
I developed a library for knowing the real viewport size for desktops and mobiles browsers, because viewport sizes are inconsistents across devices and cannot rely on all the answers of that post (according to all the research I made about this) : https://github.com/pyrsmk/W
add a comment |
Sometimes you need to see the width/height changes while resizing the window and inner content.
For that I've written a little script that adds a log box that dynamicly monitors all the resizing and almost immediatly updates.
It adds a valid HTML with fixed position and high z-index, but is small enough, so you can:
- use it on an actual site
- use it for testing mobile/responsive
views
Tested on: Chrome 40, IE11, but it is highly possible to work on other/older browsers too ... :)
function gebID(id){ return document.getElementById(id); }
function gebTN(tagName, parentEl){
if( typeof parentEl == "undefined" ) var parentEl = document;
return parentEl.getElementsByTagName(tagName);
}
function setStyleToTags(parentEl, tagName, styleString){
var tags = gebTN(tagName, parentEl);
for( var i = 0; i<tags.length; i++ ) tags[i].setAttribute('style', styleString);
}
function testSizes(){
gebID( 'screen.Width' ).innerHTML = screen.width;
gebID( 'screen.Height' ).innerHTML = screen.height;
gebID( 'window.Width' ).innerHTML = window.innerWidth;
gebID( 'window.Height' ).innerHTML = window.innerHeight;
gebID( 'documentElement.Width' ).innerHTML = document.documentElement.clientWidth;
gebID( 'documentElement.Height' ).innerHTML = document.documentElement.clientHeight;
gebID( 'body.Width' ).innerHTML = gebTN("body")[0].clientWidth;
gebID( 'body.Height' ).innerHTML = gebTN("body")[0].clientHeight;
}
var table = document.createElement('table');
table.innerHTML =
"<tr><th>SOURCE</th><th>WIDTH</th><th>x</th><th>HEIGHT</th></tr>"
+"<tr><td>screen</td><td id='screen.Width' /><td>x</td><td id='screen.Height' /></tr>"
+"<tr><td>window</td><td id='window.Width' /><td>x</td><td id='window.Height' /></tr>"
+"<tr><td>document<br>.documentElement</td><td id='documentElement.Width' /><td>x</td><td id='documentElement.Height' /></tr>"
+"<tr><td>document.body</td><td id='body.Width' /><td>x</td><td id='body.Height' /></tr>"
;
gebTN("body")[0].appendChild( table );
table.setAttribute(
'style',
"border: 2px solid black !important; position: fixed !important;"
+"left: 50% !important; top: 0px !important; padding:10px !important;"
+"width: 150px !important; font-size:18px; !important"
+"white-space: pre !important; font-family: monospace !important;"
+"z-index: 9999 !important;background: white !important;"
);
setStyleToTags(table, "td", "color: black !important; border: none !important; padding: 5px !important; text-align:center !important;");
setStyleToTags(table, "th", "color: black !important; border: none !important; padding: 5px !important; text-align:center !important;");
table.style.setProperty( 'margin-left', '-'+( table.clientWidth / 2 )+'px' );
setInterval( testSizes, 200 );
EDIT: Now styles are applied only to logger table element - not to all tables - also this is a jQuery-free solution :)
add a comment |
You can use the Screen object to get this.
The following is an example of what it would return:
Screen {
availWidth: 1920,
availHeight: 1040,
width: 1920,
height: 1080,
colorDepth: 24,
pixelDepth: 24,
top: 414,
left: 1920,
availTop: 414,
availLeft: 1920
}
To get your screenWidth variable, just use screen.width, same with screenHeight, you would just use screen.height.
To get your window width and height, it would be screen.availWidth or screen.availHeight respectively.
For the pageX and pageY variables, use window.screenX or Y. Note that this is from the VERY LEFT/TOP OF YOUR LEFT/TOP-est SCREEN. So if you have two screens of width 1920 then a window 500px from the left of the right screen would have an X value of 2420 (1920+500). screen.width/height, however, display the CURRENT screen's width or height.
To get the width and height of your page, use jQuery's $(window).height() or $(window).width().
Again using jQuery, use $("html").offset().top and $("html").offset().left for your pageX and pageY values.
add a comment |
here is my solution!
// innerWidth
const screen_viewport_inner = () => {
let w = window,
i = `inner`;
if (!(`innerWidth` in window)) {
i = `client`;
w = document.documentElement || document.body;
}
return {
width: w[`${i}Width`],
height: w[`${i}Height`]
}
};
// outerWidth
const screen_viewport_outer = () => {
let w = window,
o = `outer`;
if (!(`outerWidth` in window)) {
o = `client`;
w = document.documentElement || document.body;
}
return {
width: w[`${o}Width`],
height: w[`${o}Height`]
}
};
// style
const console_color = `
color: rgba(0,255,0,0.7);
font-size: 1.5rem;
border: 1px solid red;
`;
// testing
const test = () => {
let i_obj = screen_viewport_inner();
console.log(`%c screen_viewport_inner = n`, console_color, JSON.stringify(i_obj, null, 4));
let o_obj = screen_viewport_outer();
console.log(`%c screen_viewport_outer = n`, console_color, JSON.stringify(o_obj, null, 4));
};
// IIFE
(() => {
test();
})();
!screen-ok
– user8202629
Sep 13 '17 at 4:47
add a comment |
We can now safely use alone the native javascript window api in all browsers, without the window context.
The syntax is somewhat pretty clear!
n = "<i>px</i><br>" /* separator */
dp = devicePixelRatio /* device zoom level */
body = (() => { document.body.innerHTML = /* ready! */
"Device zoom level: " + dp
+n+ "Screen width: " + screen.width
*dp+n+ "Screen height: "+ screen.height
*dp+n+ "Document frame height: " + innerHeight
*dp+n+ "Document frame width: " + innerWidth *dp+n+ "Parent document height: "+ outerHeight *dp+n+ "Parent document width: "+ outerWidth *dp+n+ "Window available height: "+ screen.availHeight *dp+n+ "Window available width: "+ screen.availWidth *dp+n+ "Document frame max scrollable X: "+ scrollMaxX *dp+n+ "Document frame max scrollable Y: "+ scrollMaxY
*dp+n+ "Distance from left screen to window: "+ screenX
*dp+n+ "Distance from top screen to window: "+ screenY
*dp+n
})()To get accurate results in every browsers and devices, results must be multiplied by the devicePixelRatio.
The Window property devicePixelRatio returns the ratio of the
resolution in physical pixels to the resolution in CSS pixels for the
current display device. This value could also be interpreted as the
ratio of pixel sizes: the size of one CSS pixel to the size of one
physical pixel. In simpler terms, this tells the browser how many of
the screen's actual pixels should be used to draw a single CSS pixel.
This is useful when dealing with the difference between rendering on a
standard display versus a HiDPI or Retina display, which use more
screen pixels to draw the same objects, resulting in a sharper image.
There is no way to be notified when this value is changed (which can
happen, for example, if the user drags the window to a display with a
different pixel density). Since there are no callbacks or events
available to detect pixel density changes, the only way to do so is to
periodically check the value of devicePixelRatio to see if it's
changed. Just don't do it too often, or you'll impact performance.
https://developer.mozilla.org/en-US/docs/Web/API/Window/devicePixelRatio
add a comment |
protected by coldspeed Dec 20 '18 at 4:44
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
16 Answers
16
active
oldest
votes
16 Answers
16
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you are using jQuery, you can get the size of the window or document using jQuery methods:
$(window).height(); // returns height of browser viewport
$(document).height(); // returns height of HTML document (same as pageHeight in screenshot)
$(window).width(); // returns width of browser viewport
$(document).width(); // returns width of HTML document (same as pageWidth in screenshot)
For screen size you can use the screen object in the following way:
screen.height;
screen.width;
1
thanks, and is there any way to get pageX, pageY, screenX, screenY?
– turtledove
Aug 10 '10 at 2:25
2
The jQuery method height() seems to work for all elements, and returns a number (46) rather than a string like css('height') ("46px").
– Chris
Feb 6 '13 at 16:02
6
When dealing with mobile Safari, sadly jQuery isn't a perfect solution to this question. See the note on line #13 at github.com/jquery/jquery/blob/master/src/dimensions.js
– Joshua
Jun 19 '13 at 17:10
8
@웃웃웃웃웃 what did you edit in the answer? according to the revisions, you didn't edit anything at all
– DanFromGermany
Jan 28 '14 at 15:31
8
@Marco Kerwitz The worst thing is that I typed "javascript get window width" and the content of this answer was on Google. A big minus one from me.
– Maciej Krawczyk
Jun 11 '16 at 7:43
|
show 16 more comments
If you are using jQuery, you can get the size of the window or document using jQuery methods:
$(window).height(); // returns height of browser viewport
$(document).height(); // returns height of HTML document (same as pageHeight in screenshot)
$(window).width(); // returns width of browser viewport
$(document).width(); // returns width of HTML document (same as pageWidth in screenshot)
For screen size you can use the screen object in the following way:
screen.height;
screen.width;
1
thanks, and is there any way to get pageX, pageY, screenX, screenY?
– turtledove
Aug 10 '10 at 2:25
2
The jQuery method height() seems to work for all elements, and returns a number (46) rather than a string like css('height') ("46px").
– Chris
Feb 6 '13 at 16:02
6
When dealing with mobile Safari, sadly jQuery isn't a perfect solution to this question. See the note on line #13 at github.com/jquery/jquery/blob/master/src/dimensions.js
– Joshua
Jun 19 '13 at 17:10
8
@웃웃웃웃웃 what did you edit in the answer? according to the revisions, you didn't edit anything at all
– DanFromGermany
Jan 28 '14 at 15:31
8
@Marco Kerwitz The worst thing is that I typed "javascript get window width" and the content of this answer was on Google. A big minus one from me.
– Maciej Krawczyk
Jun 11 '16 at 7:43
|
show 16 more comments
If you are using jQuery, you can get the size of the window or document using jQuery methods:
$(window).height(); // returns height of browser viewport
$(document).height(); // returns height of HTML document (same as pageHeight in screenshot)
$(window).width(); // returns width of browser viewport
$(document).width(); // returns width of HTML document (same as pageWidth in screenshot)
For screen size you can use the screen object in the following way:
screen.height;
screen.width;
If you are using jQuery, you can get the size of the window or document using jQuery methods:
$(window).height(); // returns height of browser viewport
$(document).height(); // returns height of HTML document (same as pageHeight in screenshot)
$(window).width(); // returns width of browser viewport
$(document).width(); // returns width of HTML document (same as pageWidth in screenshot)
For screen size you can use the screen object in the following way:
screen.height;
screen.width;
edited Nov 16 '18 at 6:49
answered Aug 9 '10 at 6:39
Ankit JaiswalAnkit Jaiswal
17.4k53259
17.4k53259
1
thanks, and is there any way to get pageX, pageY, screenX, screenY?
– turtledove
Aug 10 '10 at 2:25
2
The jQuery method height() seems to work for all elements, and returns a number (46) rather than a string like css('height') ("46px").
– Chris
Feb 6 '13 at 16:02
6
When dealing with mobile Safari, sadly jQuery isn't a perfect solution to this question. See the note on line #13 at github.com/jquery/jquery/blob/master/src/dimensions.js
– Joshua
Jun 19 '13 at 17:10
8
@웃웃웃웃웃 what did you edit in the answer? according to the revisions, you didn't edit anything at all
– DanFromGermany
Jan 28 '14 at 15:31
8
@Marco Kerwitz The worst thing is that I typed "javascript get window width" and the content of this answer was on Google. A big minus one from me.
– Maciej Krawczyk
Jun 11 '16 at 7:43
|
show 16 more comments
1
thanks, and is there any way to get pageX, pageY, screenX, screenY?
– turtledove
Aug 10 '10 at 2:25
2
The jQuery method height() seems to work for all elements, and returns a number (46) rather than a string like css('height') ("46px").
– Chris
Feb 6 '13 at 16:02
6
When dealing with mobile Safari, sadly jQuery isn't a perfect solution to this question. See the note on line #13 at github.com/jquery/jquery/blob/master/src/dimensions.js
– Joshua
Jun 19 '13 at 17:10
8
@웃웃웃웃웃 what did you edit in the answer? according to the revisions, you didn't edit anything at all
– DanFromGermany
Jan 28 '14 at 15:31
8
@Marco Kerwitz The worst thing is that I typed "javascript get window width" and the content of this answer was on Google. A big minus one from me.
– Maciej Krawczyk
Jun 11 '16 at 7:43
1
1
thanks, and is there any way to get pageX, pageY, screenX, screenY?
– turtledove
Aug 10 '10 at 2:25
thanks, and is there any way to get pageX, pageY, screenX, screenY?
– turtledove
Aug 10 '10 at 2:25
2
2
The jQuery method height() seems to work for all elements, and returns a number (
46) rather than a string like css('height') ("46px").– Chris
Feb 6 '13 at 16:02
The jQuery method height() seems to work for all elements, and returns a number (
46) rather than a string like css('height') ("46px").– Chris
Feb 6 '13 at 16:02
6
6
When dealing with mobile Safari, sadly jQuery isn't a perfect solution to this question. See the note on line #13 at github.com/jquery/jquery/blob/master/src/dimensions.js
– Joshua
Jun 19 '13 at 17:10
When dealing with mobile Safari, sadly jQuery isn't a perfect solution to this question. See the note on line #13 at github.com/jquery/jquery/blob/master/src/dimensions.js
– Joshua
Jun 19 '13 at 17:10
8
8
@웃웃웃웃웃 what did you edit in the answer? according to the revisions, you didn't edit anything at all
– DanFromGermany
Jan 28 '14 at 15:31
@웃웃웃웃웃 what did you edit in the answer? according to the revisions, you didn't edit anything at all
– DanFromGermany
Jan 28 '14 at 15:31
8
8
@Marco Kerwitz The worst thing is that I typed "javascript get window width" and the content of this answer was on Google. A big minus one from me.
– Maciej Krawczyk
Jun 11 '16 at 7:43
@Marco Kerwitz The worst thing is that I typed "javascript get window width" and the content of this answer was on Google. A big minus one from me.
– Maciej Krawczyk
Jun 11 '16 at 7:43
|
show 16 more comments
This has everything you need to know: Get viewport/window size
but in short:
var w = window,
d = document,
e = d.documentElement,
g = d.getElementsByTagName('body')[0],
x = w.innerWidth || e.clientWidth || g.clientWidth,
y = w.innerHeight|| e.clientHeight|| g.clientHeight;
alert(x + ' × ' + y);
Fiddle
53
Why notg = document.body?
– a paid nerd
Jan 27 '14 at 3:32
50
@apaidnerd: Standards defying browsers like IE8 do not support document.body. IE9, however, does.
– Michael Mikowski
Jan 27 '14 at 22:34
42
@MichaelMikowski That is not true! Even IE5 supportsdocument.body.
– Nux
Sep 27 '14 at 19:48
30
@nux I stand corrected, and I've confirmed support in IE8. I know though that at least one brower we were targeting recently did not support document.body and we had to change to use the getElementsByTagName approach. But I guess I misremembered the browser. Sorry!
– Michael Mikowski
Sep 28 '14 at 1:07
16
I would just like to very quierly remark that one-letter variable names are never helpful.
– wybe
Jun 2 '18 at 23:27
|
show 7 more comments
This has everything you need to know: Get viewport/window size
but in short:
var w = window,
d = document,
e = d.documentElement,
g = d.getElementsByTagName('body')[0],
x = w.innerWidth || e.clientWidth || g.clientWidth,
y = w.innerHeight|| e.clientHeight|| g.clientHeight;
alert(x + ' × ' + y);
Fiddle
53
Why notg = document.body?
– a paid nerd
Jan 27 '14 at 3:32
50
@apaidnerd: Standards defying browsers like IE8 do not support document.body. IE9, however, does.
– Michael Mikowski
Jan 27 '14 at 22:34
42
@MichaelMikowski That is not true! Even IE5 supportsdocument.body.
– Nux
Sep 27 '14 at 19:48
30
@nux I stand corrected, and I've confirmed support in IE8. I know though that at least one brower we were targeting recently did not support document.body and we had to change to use the getElementsByTagName approach. But I guess I misremembered the browser. Sorry!
– Michael Mikowski
Sep 28 '14 at 1:07
16
I would just like to very quierly remark that one-letter variable names are never helpful.
– wybe
Jun 2 '18 at 23:27
|
show 7 more comments
This has everything you need to know: Get viewport/window size
but in short:
var w = window,
d = document,
e = d.documentElement,
g = d.getElementsByTagName('body')[0],
x = w.innerWidth || e.clientWidth || g.clientWidth,
y = w.innerHeight|| e.clientHeight|| g.clientHeight;
alert(x + ' × ' + y);
Fiddle
This has everything you need to know: Get viewport/window size
but in short:
var w = window,
d = document,
e = d.documentElement,
g = d.getElementsByTagName('body')[0],
x = w.innerWidth || e.clientWidth || g.clientWidth,
y = w.innerHeight|| e.clientHeight|| g.clientHeight;
alert(x + ' × ' + y);
Fiddle
edited Mar 20 at 15:39
wjandrea
1,2811331
1,2811331
answered Jul 31 '12 at 15:52
sidonaldsonsidonaldson
14.1k73749
14.1k73749
53
Why notg = document.body?
– a paid nerd
Jan 27 '14 at 3:32
50
@apaidnerd: Standards defying browsers like IE8 do not support document.body. IE9, however, does.
– Michael Mikowski
Jan 27 '14 at 22:34
42
@MichaelMikowski That is not true! Even IE5 supportsdocument.body.
– Nux
Sep 27 '14 at 19:48
30
@nux I stand corrected, and I've confirmed support in IE8. I know though that at least one brower we were targeting recently did not support document.body and we had to change to use the getElementsByTagName approach. But I guess I misremembered the browser. Sorry!
– Michael Mikowski
Sep 28 '14 at 1:07
16
I would just like to very quierly remark that one-letter variable names are never helpful.
– wybe
Jun 2 '18 at 23:27
|
show 7 more comments
53
Why notg = document.body?
– a paid nerd
Jan 27 '14 at 3:32
50
@apaidnerd: Standards defying browsers like IE8 do not support document.body. IE9, however, does.
– Michael Mikowski
Jan 27 '14 at 22:34
42
@MichaelMikowski That is not true! Even IE5 supportsdocument.body.
– Nux
Sep 27 '14 at 19:48
30
@nux I stand corrected, and I've confirmed support in IE8. I know though that at least one brower we were targeting recently did not support document.body and we had to change to use the getElementsByTagName approach. But I guess I misremembered the browser. Sorry!
– Michael Mikowski
Sep 28 '14 at 1:07
16
I would just like to very quierly remark that one-letter variable names are never helpful.
– wybe
Jun 2 '18 at 23:27
53
53
Why not
g = document.body ?– a paid nerd
Jan 27 '14 at 3:32
Why not
g = document.body ?– a paid nerd
Jan 27 '14 at 3:32
50
50
@apaidnerd: Standards defying browsers like IE8 do not support document.body. IE9, however, does.
– Michael Mikowski
Jan 27 '14 at 22:34
@apaidnerd: Standards defying browsers like IE8 do not support document.body. IE9, however, does.
– Michael Mikowski
Jan 27 '14 at 22:34
42
42
@MichaelMikowski That is not true! Even IE5 supports
document.body.– Nux
Sep 27 '14 at 19:48
@MichaelMikowski That is not true! Even IE5 supports
document.body.– Nux
Sep 27 '14 at 19:48
30
30
@nux I stand corrected, and I've confirmed support in IE8. I know though that at least one brower we were targeting recently did not support document.body and we had to change to use the getElementsByTagName approach. But I guess I misremembered the browser. Sorry!
– Michael Mikowski
Sep 28 '14 at 1:07
@nux I stand corrected, and I've confirmed support in IE8. I know though that at least one brower we were targeting recently did not support document.body and we had to change to use the getElementsByTagName approach. But I guess I misremembered the browser. Sorry!
– Michael Mikowski
Sep 28 '14 at 1:07
16
16
I would just like to very quierly remark that one-letter variable names are never helpful.
– wybe
Jun 2 '18 at 23:27
I would just like to very quierly remark that one-letter variable names are never helpful.
– wybe
Jun 2 '18 at 23:27
|
show 7 more comments
Here is a cross browser solution with pure JavaScript (Source):
var width = window.innerWidth
|| document.documentElement.clientWidth
|| document.body.clientWidth;
var height = window.innerHeight
|| document.documentElement.clientHeight
|| document.body.clientHeight;
9
This is better because if you are able to call the script early enough in the loading process (often the idea), then thebody elementwill return a value ofundefinedas the dom isn't loaded yet.
– dgo
Aug 14 '15 at 19:00
11
HA! Old thread but thanks for that! I guess I'm one of those old "idiots" that tries to support at least back to IE8 when possible, for the benefit of the surprising number of older home users who will never stop using XP until their machines catch fire. I get tired of asking questions and instead of getting an answer, getting down-voted with only "STOP SUPPORTING IE8!!" as a comment. Again thanks! This solved a problem for me in a pure javascript photo zoom I had done. Its a little slow on IE8, but now at least it works!!! :-)
– Randy
Sep 15 '16 at 20:32
great it is a lot better than rest.
– vinayak_shahdeo
15 hours ago
add a comment |
Here is a cross browser solution with pure JavaScript (Source):
var width = window.innerWidth
|| document.documentElement.clientWidth
|| document.body.clientWidth;
var height = window.innerHeight
|| document.documentElement.clientHeight
|| document.body.clientHeight;
9
This is better because if you are able to call the script early enough in the loading process (often the idea), then thebody elementwill return a value ofundefinedas the dom isn't loaded yet.
– dgo
Aug 14 '15 at 19:00
11
HA! Old thread but thanks for that! I guess I'm one of those old "idiots" that tries to support at least back to IE8 when possible, for the benefit of the surprising number of older home users who will never stop using XP until their machines catch fire. I get tired of asking questions and instead of getting an answer, getting down-voted with only "STOP SUPPORTING IE8!!" as a comment. Again thanks! This solved a problem for me in a pure javascript photo zoom I had done. Its a little slow on IE8, but now at least it works!!! :-)
– Randy
Sep 15 '16 at 20:32
great it is a lot better than rest.
– vinayak_shahdeo
15 hours ago
add a comment |
Here is a cross browser solution with pure JavaScript (Source):
var width = window.innerWidth
|| document.documentElement.clientWidth
|| document.body.clientWidth;
var height = window.innerHeight
|| document.documentElement.clientHeight
|| document.body.clientHeight;
Here is a cross browser solution with pure JavaScript (Source):
var width = window.innerWidth
|| document.documentElement.clientWidth
|| document.body.clientWidth;
var height = window.innerHeight
|| document.documentElement.clientHeight
|| document.body.clientHeight;
edited Mar 29 '15 at 13:46
answered Jan 30 '15 at 17:44
confileconfile
14.8k36151291
14.8k36151291
9
This is better because if you are able to call the script early enough in the loading process (often the idea), then thebody elementwill return a value ofundefinedas the dom isn't loaded yet.
– dgo
Aug 14 '15 at 19:00
11
HA! Old thread but thanks for that! I guess I'm one of those old "idiots" that tries to support at least back to IE8 when possible, for the benefit of the surprising number of older home users who will never stop using XP until their machines catch fire. I get tired of asking questions and instead of getting an answer, getting down-voted with only "STOP SUPPORTING IE8!!" as a comment. Again thanks! This solved a problem for me in a pure javascript photo zoom I had done. Its a little slow on IE8, but now at least it works!!! :-)
– Randy
Sep 15 '16 at 20:32
great it is a lot better than rest.
– vinayak_shahdeo
15 hours ago
add a comment |
9
This is better because if you are able to call the script early enough in the loading process (often the idea), then thebody elementwill return a value ofundefinedas the dom isn't loaded yet.
– dgo
Aug 14 '15 at 19:00
11
HA! Old thread but thanks for that! I guess I'm one of those old "idiots" that tries to support at least back to IE8 when possible, for the benefit of the surprising number of older home users who will never stop using XP until their machines catch fire. I get tired of asking questions and instead of getting an answer, getting down-voted with only "STOP SUPPORTING IE8!!" as a comment. Again thanks! This solved a problem for me in a pure javascript photo zoom I had done. Its a little slow on IE8, but now at least it works!!! :-)
– Randy
Sep 15 '16 at 20:32
great it is a lot better than rest.
– vinayak_shahdeo
15 hours ago
9
9
This is better because if you are able to call the script early enough in the loading process (often the idea), then the
body element will return a value of undefined as the dom isn't loaded yet.– dgo
Aug 14 '15 at 19:00
This is better because if you are able to call the script early enough in the loading process (often the idea), then the
body element will return a value of undefined as the dom isn't loaded yet.– dgo
Aug 14 '15 at 19:00
11
11
HA! Old thread but thanks for that! I guess I'm one of those old "idiots" that tries to support at least back to IE8 when possible, for the benefit of the surprising number of older home users who will never stop using XP until their machines catch fire. I get tired of asking questions and instead of getting an answer, getting down-voted with only "STOP SUPPORTING IE8!!" as a comment. Again thanks! This solved a problem for me in a pure javascript photo zoom I had done. Its a little slow on IE8, but now at least it works!!! :-)
– Randy
Sep 15 '16 at 20:32
HA! Old thread but thanks for that! I guess I'm one of those old "idiots" that tries to support at least back to IE8 when possible, for the benefit of the surprising number of older home users who will never stop using XP until their machines catch fire. I get tired of asking questions and instead of getting an answer, getting down-voted with only "STOP SUPPORTING IE8!!" as a comment. Again thanks! This solved a problem for me in a pure javascript photo zoom I had done. Its a little slow on IE8, but now at least it works!!! :-)
– Randy
Sep 15 '16 at 20:32
great it is a lot better than rest.
– vinayak_shahdeo
15 hours ago
great it is a lot better than rest.
– vinayak_shahdeo
15 hours ago
add a comment |
A non-jQuery way to get the available screen dimension. window.screen.width/height has already been put up, but for responsive webdesign and completeness sake I think its worth to mention those attributes:
alert(window.screen.availWidth);
alert(window.screen.availHeight);
http://www.quirksmode.org/dom/w3c_cssom.html#t10 :
availWidth and availHeight - The available width and height on the
screen (excluding OS taskbars and such).
same as width screen.height - on android chrome browser shows divided by pixel ratio :(
– Darius.V
Jan 30 '15 at 9:52
2
window.screen.availHeightseems to assume full screen mode so that the normal screen mode forces scrolling (tested in Firefox and Chrome).
– Suzana
Mar 21 '15 at 15:13
@Suzana_K then usewindow.screen.heightinstead.
– Vallentin
Apr 2 '16 at 1:40
add a comment |
A non-jQuery way to get the available screen dimension. window.screen.width/height has already been put up, but for responsive webdesign and completeness sake I think its worth to mention those attributes:
alert(window.screen.availWidth);
alert(window.screen.availHeight);
http://www.quirksmode.org/dom/w3c_cssom.html#t10 :
availWidth and availHeight - The available width and height on the
screen (excluding OS taskbars and such).
same as width screen.height - on android chrome browser shows divided by pixel ratio :(
– Darius.V
Jan 30 '15 at 9:52
2
window.screen.availHeightseems to assume full screen mode so that the normal screen mode forces scrolling (tested in Firefox and Chrome).
– Suzana
Mar 21 '15 at 15:13
@Suzana_K then usewindow.screen.heightinstead.
– Vallentin
Apr 2 '16 at 1:40
add a comment |
A non-jQuery way to get the available screen dimension. window.screen.width/height has already been put up, but for responsive webdesign and completeness sake I think its worth to mention those attributes:
alert(window.screen.availWidth);
alert(window.screen.availHeight);
http://www.quirksmode.org/dom/w3c_cssom.html#t10 :
availWidth and availHeight - The available width and height on the
screen (excluding OS taskbars and such).
A non-jQuery way to get the available screen dimension. window.screen.width/height has already been put up, but for responsive webdesign and completeness sake I think its worth to mention those attributes:
alert(window.screen.availWidth);
alert(window.screen.availHeight);
http://www.quirksmode.org/dom/w3c_cssom.html#t10 :
availWidth and availHeight - The available width and height on the
screen (excluding OS taskbars and such).
edited Feb 27 '14 at 22:29
answered Jun 20 '13 at 10:06
DanFromGermanyDanFromGermany
19.9k74796
19.9k74796
same as width screen.height - on android chrome browser shows divided by pixel ratio :(
– Darius.V
Jan 30 '15 at 9:52
2
window.screen.availHeightseems to assume full screen mode so that the normal screen mode forces scrolling (tested in Firefox and Chrome).
– Suzana
Mar 21 '15 at 15:13
@Suzana_K then usewindow.screen.heightinstead.
– Vallentin
Apr 2 '16 at 1:40
add a comment |
same as width screen.height - on android chrome browser shows divided by pixel ratio :(
– Darius.V
Jan 30 '15 at 9:52
2
window.screen.availHeightseems to assume full screen mode so that the normal screen mode forces scrolling (tested in Firefox and Chrome).
– Suzana
Mar 21 '15 at 15:13
@Suzana_K then usewindow.screen.heightinstead.
– Vallentin
Apr 2 '16 at 1:40
same as width screen.height - on android chrome browser shows divided by pixel ratio :(
– Darius.V
Jan 30 '15 at 9:52
same as width screen.height - on android chrome browser shows divided by pixel ratio :(
– Darius.V
Jan 30 '15 at 9:52
2
2
window.screen.availHeight seems to assume full screen mode so that the normal screen mode forces scrolling (tested in Firefox and Chrome).– Suzana
Mar 21 '15 at 15:13
window.screen.availHeight seems to assume full screen mode so that the normal screen mode forces scrolling (tested in Firefox and Chrome).– Suzana
Mar 21 '15 at 15:13
@Suzana_K then use
window.screen.height instead.– Vallentin
Apr 2 '16 at 1:40
@Suzana_K then use
window.screen.height instead.– Vallentin
Apr 2 '16 at 1:40
add a comment |
But when we talk about responsive screens and if we want to handle it using jQuery for some reason,
window.innerWidth, window.innerHeight
gives the correct measurement. Even it removes the scroll-bar's extra space and we don't need to worry about adjusting that space :)
2
This should be the accepted answer with loads of upvotes. A much more useful answer than the currently accepted answer, and it also doesn't depend on jQuery.
– developerbmw
Apr 29 '15 at 23:25
4
Unfortunately,window.innerWidthandwindow.innerHeightfail to take the size of the scroll bar into account. If scroll bars are present, these methods return wrong results for the window size in nearly all desktop browsers. See stackoverflow.com/a/31655549/508355
– hashchange
Aug 25 '15 at 11:13
add a comment |
But when we talk about responsive screens and if we want to handle it using jQuery for some reason,
window.innerWidth, window.innerHeight
gives the correct measurement. Even it removes the scroll-bar's extra space and we don't need to worry about adjusting that space :)
2
This should be the accepted answer with loads of upvotes. A much more useful answer than the currently accepted answer, and it also doesn't depend on jQuery.
– developerbmw
Apr 29 '15 at 23:25
4
Unfortunately,window.innerWidthandwindow.innerHeightfail to take the size of the scroll bar into account. If scroll bars are present, these methods return wrong results for the window size in nearly all desktop browsers. See stackoverflow.com/a/31655549/508355
– hashchange
Aug 25 '15 at 11:13
add a comment |
But when we talk about responsive screens and if we want to handle it using jQuery for some reason,
window.innerWidth, window.innerHeight
gives the correct measurement. Even it removes the scroll-bar's extra space and we don't need to worry about adjusting that space :)
But when we talk about responsive screens and if we want to handle it using jQuery for some reason,
window.innerWidth, window.innerHeight
gives the correct measurement. Even it removes the scroll-bar's extra space and we don't need to worry about adjusting that space :)
answered Apr 22 '15 at 7:29
Aabha PandeyAabha Pandey
65657
65657
2
This should be the accepted answer with loads of upvotes. A much more useful answer than the currently accepted answer, and it also doesn't depend on jQuery.
– developerbmw
Apr 29 '15 at 23:25
4
Unfortunately,window.innerWidthandwindow.innerHeightfail to take the size of the scroll bar into account. If scroll bars are present, these methods return wrong results for the window size in nearly all desktop browsers. See stackoverflow.com/a/31655549/508355
– hashchange
Aug 25 '15 at 11:13
add a comment |
2
This should be the accepted answer with loads of upvotes. A much more useful answer than the currently accepted answer, and it also doesn't depend on jQuery.
– developerbmw
Apr 29 '15 at 23:25
4
Unfortunately,window.innerWidthandwindow.innerHeightfail to take the size of the scroll bar into account. If scroll bars are present, these methods return wrong results for the window size in nearly all desktop browsers. See stackoverflow.com/a/31655549/508355
– hashchange
Aug 25 '15 at 11:13
2
2
This should be the accepted answer with loads of upvotes. A much more useful answer than the currently accepted answer, and it also doesn't depend on jQuery.
– developerbmw
Apr 29 '15 at 23:25
This should be the accepted answer with loads of upvotes. A much more useful answer than the currently accepted answer, and it also doesn't depend on jQuery.
– developerbmw
Apr 29 '15 at 23:25
4
4
Unfortunately,
window.innerWidth and window.innerHeight fail to take the size of the scroll bar into account. If scroll bars are present, these methods return wrong results for the window size in nearly all desktop browsers. See stackoverflow.com/a/31655549/508355– hashchange
Aug 25 '15 at 11:13
Unfortunately,
window.innerWidth and window.innerHeight fail to take the size of the scroll bar into account. If scroll bars are present, these methods return wrong results for the window size in nearly all desktop browsers. See stackoverflow.com/a/31655549/508355– hashchange
Aug 25 '15 at 11:13
add a comment |
To check height and width of your current loaded page of any website using "console" or after clicking "Inspect".
step 1: Click the right button of mouse and click on 'Inspect' and then click 'console'
step 2: Make sure that your browser screen should be not in 'maximize' mode. If the browser screen is in 'maximize' mode, you need to first click the maximize button (present either at right or left top corner) and un-maximize it.
step 3: Now, write the following after the greater than sign ('>') i.e.
> window.innerWidth
output : your present window width in px (say 749)
> window.innerHeight
output : your present window height in px (say 359)
1
Why isn't this the selected answer?
– Iulian Onofrei
Oct 8 '16 at 18:30
1
@IulianOnofrei because it does not fully answer the original set of questions. Joke being, it is the correct answer for me.
– rob
Jan 18 '17 at 15:06
innerWidth and innerHeight are only accurate if the viewport scale is 1. This is the default, but it can be inadvertently changed using css transforms.
– Victor Stoddard
Mar 14 '18 at 0:49
"window.innerWidth" as opposed to "screen.width" worked for me to determine size of user's browser - thank you!
– Kevin Pajak
Jul 24 '18 at 11:02
add a comment |
To check height and width of your current loaded page of any website using "console" or after clicking "Inspect".
step 1: Click the right button of mouse and click on 'Inspect' and then click 'console'
step 2: Make sure that your browser screen should be not in 'maximize' mode. If the browser screen is in 'maximize' mode, you need to first click the maximize button (present either at right or left top corner) and un-maximize it.
step 3: Now, write the following after the greater than sign ('>') i.e.
> window.innerWidth
output : your present window width in px (say 749)
> window.innerHeight
output : your present window height in px (say 359)
1
Why isn't this the selected answer?
– Iulian Onofrei
Oct 8 '16 at 18:30
1
@IulianOnofrei because it does not fully answer the original set of questions. Joke being, it is the correct answer for me.
– rob
Jan 18 '17 at 15:06
innerWidth and innerHeight are only accurate if the viewport scale is 1. This is the default, but it can be inadvertently changed using css transforms.
– Victor Stoddard
Mar 14 '18 at 0:49
"window.innerWidth" as opposed to "screen.width" worked for me to determine size of user's browser - thank you!
– Kevin Pajak
Jul 24 '18 at 11:02
add a comment |
To check height and width of your current loaded page of any website using "console" or after clicking "Inspect".
step 1: Click the right button of mouse and click on 'Inspect' and then click 'console'
step 2: Make sure that your browser screen should be not in 'maximize' mode. If the browser screen is in 'maximize' mode, you need to first click the maximize button (present either at right or left top corner) and un-maximize it.
step 3: Now, write the following after the greater than sign ('>') i.e.
> window.innerWidth
output : your present window width in px (say 749)
> window.innerHeight
output : your present window height in px (say 359)
To check height and width of your current loaded page of any website using "console" or after clicking "Inspect".
step 1: Click the right button of mouse and click on 'Inspect' and then click 'console'
step 2: Make sure that your browser screen should be not in 'maximize' mode. If the browser screen is in 'maximize' mode, you need to first click the maximize button (present either at right or left top corner) and un-maximize it.
step 3: Now, write the following after the greater than sign ('>') i.e.
> window.innerWidth
output : your present window width in px (say 749)
> window.innerHeight
output : your present window height in px (say 359)
answered Jul 9 '16 at 4:22
solanki...solanki...
1,46911020
1,46911020
1
Why isn't this the selected answer?
– Iulian Onofrei
Oct 8 '16 at 18:30
1
@IulianOnofrei because it does not fully answer the original set of questions. Joke being, it is the correct answer for me.
– rob
Jan 18 '17 at 15:06
innerWidth and innerHeight are only accurate if the viewport scale is 1. This is the default, but it can be inadvertently changed using css transforms.
– Victor Stoddard
Mar 14 '18 at 0:49
"window.innerWidth" as opposed to "screen.width" worked for me to determine size of user's browser - thank you!
– Kevin Pajak
Jul 24 '18 at 11:02
add a comment |
1
Why isn't this the selected answer?
– Iulian Onofrei
Oct 8 '16 at 18:30
1
@IulianOnofrei because it does not fully answer the original set of questions. Joke being, it is the correct answer for me.
– rob
Jan 18 '17 at 15:06
innerWidth and innerHeight are only accurate if the viewport scale is 1. This is the default, but it can be inadvertently changed using css transforms.
– Victor Stoddard
Mar 14 '18 at 0:49
"window.innerWidth" as opposed to "screen.width" worked for me to determine size of user's browser - thank you!
– Kevin Pajak
Jul 24 '18 at 11:02
1
1
Why isn't this the selected answer?
– Iulian Onofrei
Oct 8 '16 at 18:30
Why isn't this the selected answer?
– Iulian Onofrei
Oct 8 '16 at 18:30
1
1
@IulianOnofrei because it does not fully answer the original set of questions. Joke being, it is the correct answer for me.
– rob
Jan 18 '17 at 15:06
@IulianOnofrei because it does not fully answer the original set of questions. Joke being, it is the correct answer for me.
– rob
Jan 18 '17 at 15:06
innerWidth and innerHeight are only accurate if the viewport scale is 1. This is the default, but it can be inadvertently changed using css transforms.
– Victor Stoddard
Mar 14 '18 at 0:49
innerWidth and innerHeight are only accurate if the viewport scale is 1. This is the default, but it can be inadvertently changed using css transforms.
– Victor Stoddard
Mar 14 '18 at 0:49
"window.innerWidth" as opposed to "screen.width" worked for me to determine size of user's browser - thank you!
– Kevin Pajak
Jul 24 '18 at 11:02
"window.innerWidth" as opposed to "screen.width" worked for me to determine size of user's browser - thank you!
– Kevin Pajak
Jul 24 '18 at 11:02
add a comment |
You can also get the WINDOW width and height, avoiding browser toolbars and other stuff. It is the real usable area in browser's window.
To do this, use:
window.innerWidth and window.innerHeight properties (see doc at w3schools).
In most cases it will be the best way, in example, to display a perfectly centred floating modal dialog. It allows you to calculate positions on window, no matter which resolution orientation or window size is using the browser.
2
This is not supported in ie8 nor ie7, according to w3schools. Also, you must watch out when you link to w3schools. See meta.stackexchange.com/questions/87678/…
– thecarpy
Feb 4 '15 at 6:31
add a comment |
You can also get the WINDOW width and height, avoiding browser toolbars and other stuff. It is the real usable area in browser's window.
To do this, use:
window.innerWidth and window.innerHeight properties (see doc at w3schools).
In most cases it will be the best way, in example, to display a perfectly centred floating modal dialog. It allows you to calculate positions on window, no matter which resolution orientation or window size is using the browser.
2
This is not supported in ie8 nor ie7, according to w3schools. Also, you must watch out when you link to w3schools. See meta.stackexchange.com/questions/87678/…
– thecarpy
Feb 4 '15 at 6:31
add a comment |
You can also get the WINDOW width and height, avoiding browser toolbars and other stuff. It is the real usable area in browser's window.
To do this, use:
window.innerWidth and window.innerHeight properties (see doc at w3schools).
In most cases it will be the best way, in example, to display a perfectly centred floating modal dialog. It allows you to calculate positions on window, no matter which resolution orientation or window size is using the browser.
You can also get the WINDOW width and height, avoiding browser toolbars and other stuff. It is the real usable area in browser's window.
To do this, use:
window.innerWidth and window.innerHeight properties (see doc at w3schools).
In most cases it will be the best way, in example, to display a perfectly centred floating modal dialog. It allows you to calculate positions on window, no matter which resolution orientation or window size is using the browser.
answered Aug 21 '14 at 10:44
serfer2serfer2
1,65111515
1,65111515
2
This is not supported in ie8 nor ie7, according to w3schools. Also, you must watch out when you link to w3schools. See meta.stackexchange.com/questions/87678/…
– thecarpy
Feb 4 '15 at 6:31
add a comment |
2
This is not supported in ie8 nor ie7, according to w3schools. Also, you must watch out when you link to w3schools. See meta.stackexchange.com/questions/87678/…
– thecarpy
Feb 4 '15 at 6:31
2
2
This is not supported in ie8 nor ie7, according to w3schools. Also, you must watch out when you link to w3schools. See meta.stackexchange.com/questions/87678/…
– thecarpy
Feb 4 '15 at 6:31
This is not supported in ie8 nor ie7, according to w3schools. Also, you must watch out when you link to w3schools. See meta.stackexchange.com/questions/87678/…
– thecarpy
Feb 4 '15 at 6:31
add a comment |
function wndsize(){
var w = 0;var h = 0;
//IE
if(!window.innerWidth){
if(!(document.documentElement.clientWidth == 0)){
//strict mode
w = document.documentElement.clientWidth;h = document.documentElement.clientHeight;
} else{
//quirks mode
w = document.body.clientWidth;h = document.body.clientHeight;
}
} else {
//w3c
w = window.innerWidth;h = window.innerHeight;
}
return {width:w,height:h};
}
function wndcent(){
var hWnd = (arguments[0] != null) ? arguments[0] : {width:0,height:0};
var _x = 0;var _y = 0;var offsetX = 0;var offsetY = 0;
//IE
if(!window.pageYOffset){
//strict mode
if(!(document.documentElement.scrollTop == 0)){offsetY = document.documentElement.scrollTop;offsetX = document.documentElement.scrollLeft;}
//quirks mode
else{offsetY = document.body.scrollTop;offsetX = document.body.scrollLeft;}}
//w3c
else{offsetX = window.pageXOffset;offsetY = window.pageYOffset;}_x = ((wndsize().width-hWnd.width)/2)+offsetX;_y = ((wndsize().height-hWnd.height)/2)+offsetY;
return{x:_x,y:_y};
}
var center = wndcent({width:350,height:350});
document.write(center.x+';<br>');
document.write(center.y+';<br>');
document.write('<DIV align="center" id="rich_ad" style="Z-INDEX: 10; left:'+center.x+'px;WIDTH: 350px; POSITION: absolute; TOP: '+center.y+'px; HEIGHT: 350px"><!--К сожалению, у Вас не установлен flash плеер.--></div>');
add a comment |
function wndsize(){
var w = 0;var h = 0;
//IE
if(!window.innerWidth){
if(!(document.documentElement.clientWidth == 0)){
//strict mode
w = document.documentElement.clientWidth;h = document.documentElement.clientHeight;
} else{
//quirks mode
w = document.body.clientWidth;h = document.body.clientHeight;
}
} else {
//w3c
w = window.innerWidth;h = window.innerHeight;
}
return {width:w,height:h};
}
function wndcent(){
var hWnd = (arguments[0] != null) ? arguments[0] : {width:0,height:0};
var _x = 0;var _y = 0;var offsetX = 0;var offsetY = 0;
//IE
if(!window.pageYOffset){
//strict mode
if(!(document.documentElement.scrollTop == 0)){offsetY = document.documentElement.scrollTop;offsetX = document.documentElement.scrollLeft;}
//quirks mode
else{offsetY = document.body.scrollTop;offsetX = document.body.scrollLeft;}}
//w3c
else{offsetX = window.pageXOffset;offsetY = window.pageYOffset;}_x = ((wndsize().width-hWnd.width)/2)+offsetX;_y = ((wndsize().height-hWnd.height)/2)+offsetY;
return{x:_x,y:_y};
}
var center = wndcent({width:350,height:350});
document.write(center.x+';<br>');
document.write(center.y+';<br>');
document.write('<DIV align="center" id="rich_ad" style="Z-INDEX: 10; left:'+center.x+'px;WIDTH: 350px; POSITION: absolute; TOP: '+center.y+'px; HEIGHT: 350px"><!--К сожалению, у Вас не установлен flash плеер.--></div>');
add a comment |
function wndsize(){
var w = 0;var h = 0;
//IE
if(!window.innerWidth){
if(!(document.documentElement.clientWidth == 0)){
//strict mode
w = document.documentElement.clientWidth;h = document.documentElement.clientHeight;
} else{
//quirks mode
w = document.body.clientWidth;h = document.body.clientHeight;
}
} else {
//w3c
w = window.innerWidth;h = window.innerHeight;
}
return {width:w,height:h};
}
function wndcent(){
var hWnd = (arguments[0] != null) ? arguments[0] : {width:0,height:0};
var _x = 0;var _y = 0;var offsetX = 0;var offsetY = 0;
//IE
if(!window.pageYOffset){
//strict mode
if(!(document.documentElement.scrollTop == 0)){offsetY = document.documentElement.scrollTop;offsetX = document.documentElement.scrollLeft;}
//quirks mode
else{offsetY = document.body.scrollTop;offsetX = document.body.scrollLeft;}}
//w3c
else{offsetX = window.pageXOffset;offsetY = window.pageYOffset;}_x = ((wndsize().width-hWnd.width)/2)+offsetX;_y = ((wndsize().height-hWnd.height)/2)+offsetY;
return{x:_x,y:_y};
}
var center = wndcent({width:350,height:350});
document.write(center.x+';<br>');
document.write(center.y+';<br>');
document.write('<DIV align="center" id="rich_ad" style="Z-INDEX: 10; left:'+center.x+'px;WIDTH: 350px; POSITION: absolute; TOP: '+center.y+'px; HEIGHT: 350px"><!--К сожалению, у Вас не установлен flash плеер.--></div>');
function wndsize(){
var w = 0;var h = 0;
//IE
if(!window.innerWidth){
if(!(document.documentElement.clientWidth == 0)){
//strict mode
w = document.documentElement.clientWidth;h = document.documentElement.clientHeight;
} else{
//quirks mode
w = document.body.clientWidth;h = document.body.clientHeight;
}
} else {
//w3c
w = window.innerWidth;h = window.innerHeight;
}
return {width:w,height:h};
}
function wndcent(){
var hWnd = (arguments[0] != null) ? arguments[0] : {width:0,height:0};
var _x = 0;var _y = 0;var offsetX = 0;var offsetY = 0;
//IE
if(!window.pageYOffset){
//strict mode
if(!(document.documentElement.scrollTop == 0)){offsetY = document.documentElement.scrollTop;offsetX = document.documentElement.scrollLeft;}
//quirks mode
else{offsetY = document.body.scrollTop;offsetX = document.body.scrollLeft;}}
//w3c
else{offsetX = window.pageXOffset;offsetY = window.pageYOffset;}_x = ((wndsize().width-hWnd.width)/2)+offsetX;_y = ((wndsize().height-hWnd.height)/2)+offsetY;
return{x:_x,y:_y};
}
var center = wndcent({width:350,height:350});
document.write(center.x+';<br>');
document.write(center.y+';<br>');
document.write('<DIV align="center" id="rich_ad" style="Z-INDEX: 10; left:'+center.x+'px;WIDTH: 350px; POSITION: absolute; TOP: '+center.y+'px; HEIGHT: 350px"><!--К сожалению, у Вас не установлен flash плеер.--></div>');
edited Nov 24 '15 at 19:31
Jonatas Walker
8,51342358
8,51342358
answered Feb 11 '12 at 4:25
dudedude
21122
21122
add a comment |
add a comment |
If you need a truly bulletproof solution for the document width and height (the pageWidth and pageHeight in the picture), you might want to consider using a plugin of mine, jQuery.documentSize.
It has just one purpose: to always return the correct document size, even in scenarios when jQuery and other methods fail. Despite its name, you don't necessarily have to use jQuery – it is written in vanilla Javascript and works without jQuery, too.
Usage:
var w = $.documentWidth(),
h = $.documentHeight();
for the global document. For other documents, e.g. in an embedded iframe you have access to, pass the document as a parameter:
var w = $.documentWidth( myIframe.contentDocument ),
h = $.documentHeight( myIframe.contentDocument );
Update: now for window dimensions, too
Ever since version 1.1.0, jQuery.documentSize also handles window dimensions.
That is necessary because
$( window ).height()is buggy in iOS, to the point of being useless
$( window ).width()and$( window ).height()are unreliable on mobile because they don't handle the effects of mobile zooming.
jQuery.documentSize provides $.windowWidth() and $.windowHeight(), which solve these issues. For more, please check out the documentation.
VM2859:1 Uncaught DOMException: Blocked a frame with origin "localhost:12999" from accessing a cross-origin frame.(…) is there any way to over come this ???
– fizmhd
Jul 13 '16 at 10:44
1
You can't access iframes from a different domain, it violates the same-origin policy. Check out this answer.
– hashchange
Jul 13 '16 at 10:53
This not working if you are embedding an iframe and trying to get the height of the view port. window.innerHeight equal to the document height which is buggy. You cannot access frames. from a different domain it's not a feasible solution.
– M.Abulsoud
Dec 12 '18 at 13:47
@M.Abulsoud Provided you have access to the iframe (no CORS violation), it does work and is covered by the test suite. Use this syntax. It even works with multiple iframes nested inside each other - see this example on Codepen. If your setup works generally, but fails in a single specific browser, it might be a bug I'm not aware of. Perhaps you can open an issue in that case? Thanks.
– hashchange
Dec 12 '18 at 16:24
add a comment |
If you need a truly bulletproof solution for the document width and height (the pageWidth and pageHeight in the picture), you might want to consider using a plugin of mine, jQuery.documentSize.
It has just one purpose: to always return the correct document size, even in scenarios when jQuery and other methods fail. Despite its name, you don't necessarily have to use jQuery – it is written in vanilla Javascript and works without jQuery, too.
Usage:
var w = $.documentWidth(),
h = $.documentHeight();
for the global document. For other documents, e.g. in an embedded iframe you have access to, pass the document as a parameter:
var w = $.documentWidth( myIframe.contentDocument ),
h = $.documentHeight( myIframe.contentDocument );
Update: now for window dimensions, too
Ever since version 1.1.0, jQuery.documentSize also handles window dimensions.
That is necessary because
$( window ).height()is buggy in iOS, to the point of being useless
$( window ).width()and$( window ).height()are unreliable on mobile because they don't handle the effects of mobile zooming.
jQuery.documentSize provides $.windowWidth() and $.windowHeight(), which solve these issues. For more, please check out the documentation.
VM2859:1 Uncaught DOMException: Blocked a frame with origin "localhost:12999" from accessing a cross-origin frame.(…) is there any way to over come this ???
– fizmhd
Jul 13 '16 at 10:44
1
You can't access iframes from a different domain, it violates the same-origin policy. Check out this answer.
– hashchange
Jul 13 '16 at 10:53
This not working if you are embedding an iframe and trying to get the height of the view port. window.innerHeight equal to the document height which is buggy. You cannot access frames. from a different domain it's not a feasible solution.
– M.Abulsoud
Dec 12 '18 at 13:47
@M.Abulsoud Provided you have access to the iframe (no CORS violation), it does work and is covered by the test suite. Use this syntax. It even works with multiple iframes nested inside each other - see this example on Codepen. If your setup works generally, but fails in a single specific browser, it might be a bug I'm not aware of. Perhaps you can open an issue in that case? Thanks.
– hashchange
Dec 12 '18 at 16:24
add a comment |
If you need a truly bulletproof solution for the document width and height (the pageWidth and pageHeight in the picture), you might want to consider using a plugin of mine, jQuery.documentSize.
It has just one purpose: to always return the correct document size, even in scenarios when jQuery and other methods fail. Despite its name, you don't necessarily have to use jQuery – it is written in vanilla Javascript and works without jQuery, too.
Usage:
var w = $.documentWidth(),
h = $.documentHeight();
for the global document. For other documents, e.g. in an embedded iframe you have access to, pass the document as a parameter:
var w = $.documentWidth( myIframe.contentDocument ),
h = $.documentHeight( myIframe.contentDocument );
Update: now for window dimensions, too
Ever since version 1.1.0, jQuery.documentSize also handles window dimensions.
That is necessary because
$( window ).height()is buggy in iOS, to the point of being useless
$( window ).width()and$( window ).height()are unreliable on mobile because they don't handle the effects of mobile zooming.
jQuery.documentSize provides $.windowWidth() and $.windowHeight(), which solve these issues. For more, please check out the documentation.
If you need a truly bulletproof solution for the document width and height (the pageWidth and pageHeight in the picture), you might want to consider using a plugin of mine, jQuery.documentSize.
It has just one purpose: to always return the correct document size, even in scenarios when jQuery and other methods fail. Despite its name, you don't necessarily have to use jQuery – it is written in vanilla Javascript and works without jQuery, too.
Usage:
var w = $.documentWidth(),
h = $.documentHeight();
for the global document. For other documents, e.g. in an embedded iframe you have access to, pass the document as a parameter:
var w = $.documentWidth( myIframe.contentDocument ),
h = $.documentHeight( myIframe.contentDocument );
Update: now for window dimensions, too
Ever since version 1.1.0, jQuery.documentSize also handles window dimensions.
That is necessary because
$( window ).height()is buggy in iOS, to the point of being useless
$( window ).width()and$( window ).height()are unreliable on mobile because they don't handle the effects of mobile zooming.
jQuery.documentSize provides $.windowWidth() and $.windowHeight(), which solve these issues. For more, please check out the documentation.
edited May 23 '17 at 12:26
Community♦
11
11
answered Mar 4 '15 at 22:30
hashchangehashchange
4,6232933
4,6232933
VM2859:1 Uncaught DOMException: Blocked a frame with origin "localhost:12999" from accessing a cross-origin frame.(…) is there any way to over come this ???
– fizmhd
Jul 13 '16 at 10:44
1
You can't access iframes from a different domain, it violates the same-origin policy. Check out this answer.
– hashchange
Jul 13 '16 at 10:53
This not working if you are embedding an iframe and trying to get the height of the view port. window.innerHeight equal to the document height which is buggy. You cannot access frames. from a different domain it's not a feasible solution.
– M.Abulsoud
Dec 12 '18 at 13:47
@M.Abulsoud Provided you have access to the iframe (no CORS violation), it does work and is covered by the test suite. Use this syntax. It even works with multiple iframes nested inside each other - see this example on Codepen. If your setup works generally, but fails in a single specific browser, it might be a bug I'm not aware of. Perhaps you can open an issue in that case? Thanks.
– hashchange
Dec 12 '18 at 16:24
add a comment |
VM2859:1 Uncaught DOMException: Blocked a frame with origin "localhost:12999" from accessing a cross-origin frame.(…) is there any way to over come this ???
– fizmhd
Jul 13 '16 at 10:44
1
You can't access iframes from a different domain, it violates the same-origin policy. Check out this answer.
– hashchange
Jul 13 '16 at 10:53
This not working if you are embedding an iframe and trying to get the height of the view port. window.innerHeight equal to the document height which is buggy. You cannot access frames. from a different domain it's not a feasible solution.
– M.Abulsoud
Dec 12 '18 at 13:47
@M.Abulsoud Provided you have access to the iframe (no CORS violation), it does work and is covered by the test suite. Use this syntax. It even works with multiple iframes nested inside each other - see this example on Codepen. If your setup works generally, but fails in a single specific browser, it might be a bug I'm not aware of. Perhaps you can open an issue in that case? Thanks.
– hashchange
Dec 12 '18 at 16:24
VM2859:1 Uncaught DOMException: Blocked a frame with origin "localhost:12999" from accessing a cross-origin frame.(…) is there any way to over come this ???
– fizmhd
Jul 13 '16 at 10:44
VM2859:1 Uncaught DOMException: Blocked a frame with origin "localhost:12999" from accessing a cross-origin frame.(…) is there any way to over come this ???
– fizmhd
Jul 13 '16 at 10:44
1
1
You can't access iframes from a different domain, it violates the same-origin policy. Check out this answer.
– hashchange
Jul 13 '16 at 10:53
You can't access iframes from a different domain, it violates the same-origin policy. Check out this answer.
– hashchange
Jul 13 '16 at 10:53
This not working if you are embedding an iframe and trying to get the height of the view port. window.innerHeight equal to the document height which is buggy. You cannot access frames. from a different domain it's not a feasible solution.
– M.Abulsoud
Dec 12 '18 at 13:47
This not working if you are embedding an iframe and trying to get the height of the view port. window.innerHeight equal to the document height which is buggy. You cannot access frames. from a different domain it's not a feasible solution.
– M.Abulsoud
Dec 12 '18 at 13:47
@M.Abulsoud Provided you have access to the iframe (no CORS violation), it does work and is covered by the test suite. Use this syntax. It even works with multiple iframes nested inside each other - see this example on Codepen. If your setup works generally, but fails in a single specific browser, it might be a bug I'm not aware of. Perhaps you can open an issue in that case? Thanks.
– hashchange
Dec 12 '18 at 16:24
@M.Abulsoud Provided you have access to the iframe (no CORS violation), it does work and is covered by the test suite. Use this syntax. It even works with multiple iframes nested inside each other - see this example on Codepen. If your setup works generally, but fails in a single specific browser, it might be a bug I'm not aware of. Perhaps you can open an issue in that case? Thanks.
– hashchange
Dec 12 '18 at 16:24
add a comment |
I wrote a small javascript bookmarklet you can use to display the size. You can easily add it to your browser and whenever you click it you will see the size in the right corner of your browser window.
Here you find information how to use a bookmarklet
https://en.wikipedia.org/wiki/Bookmarklet
Bookmarklet
javascript:(function(){!function(){var i,n,e;return n=function(){var n,e,t;return t="background-color:azure; padding:1rem; position:fixed; right: 0; z-index:9999; font-size: 1.2rem;",n=i('<div style="'+t+'"></div>'),e=function(){return'<p style="margin:0;">width: '+i(window).width()+" height: "+i(window).height()+"</p>"},n.html(e()),i("body").prepend(n),i(window).resize(function(){n.html(e())})},(i=window.jQuery)?(i=window.jQuery,n()):(e=document.createElement("script"),e.src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js",e.onload=n,document.body.appendChild(e))}()}).call(this);
Original Code
The original code is in coffee:
(->
addWindowSize = ()->
style = 'background-color:azure; padding:1rem; position:fixed; right: 0; z-index:9999; font-size: 1.2rem;'
$windowSize = $('<div style="' + style + '"></div>')
getWindowSize = ->
'<p style="margin:0;">width: ' + $(window).width() + ' height: ' + $(window).height() + '</p>'
$windowSize.html getWindowSize()
$('body').prepend $windowSize
$(window).resize ->
$windowSize.html getWindowSize()
return
if !($ = window.jQuery)
# typeof jQuery=='undefined' works too
script = document.createElement('script')
script.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js'
script.onload = addWindowSize
document.body.appendChild script
else
$ = window.jQuery
addWindowSize()
)()
Basically the code is prepending a small div which updates when you resize your window.
add a comment |
I wrote a small javascript bookmarklet you can use to display the size. You can easily add it to your browser and whenever you click it you will see the size in the right corner of your browser window.
Here you find information how to use a bookmarklet
https://en.wikipedia.org/wiki/Bookmarklet
Bookmarklet
javascript:(function(){!function(){var i,n,e;return n=function(){var n,e,t;return t="background-color:azure; padding:1rem; position:fixed; right: 0; z-index:9999; font-size: 1.2rem;",n=i('<div style="'+t+'"></div>'),e=function(){return'<p style="margin:0;">width: '+i(window).width()+" height: "+i(window).height()+"</p>"},n.html(e()),i("body").prepend(n),i(window).resize(function(){n.html(e())})},(i=window.jQuery)?(i=window.jQuery,n()):(e=document.createElement("script"),e.src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js",e.onload=n,document.body.appendChild(e))}()}).call(this);
Original Code
The original code is in coffee:
(->
addWindowSize = ()->
style = 'background-color:azure; padding:1rem; position:fixed; right: 0; z-index:9999; font-size: 1.2rem;'
$windowSize = $('<div style="' + style + '"></div>')
getWindowSize = ->
'<p style="margin:0;">width: ' + $(window).width() + ' height: ' + $(window).height() + '</p>'
$windowSize.html getWindowSize()
$('body').prepend $windowSize
$(window).resize ->
$windowSize.html getWindowSize()
return
if !($ = window.jQuery)
# typeof jQuery=='undefined' works too
script = document.createElement('script')
script.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js'
script.onload = addWindowSize
document.body.appendChild script
else
$ = window.jQuery
addWindowSize()
)()
Basically the code is prepending a small div which updates when you resize your window.
add a comment |
I wrote a small javascript bookmarklet you can use to display the size. You can easily add it to your browser and whenever you click it you will see the size in the right corner of your browser window.
Here you find information how to use a bookmarklet
https://en.wikipedia.org/wiki/Bookmarklet
Bookmarklet
javascript:(function(){!function(){var i,n,e;return n=function(){var n,e,t;return t="background-color:azure; padding:1rem; position:fixed; right: 0; z-index:9999; font-size: 1.2rem;",n=i('<div style="'+t+'"></div>'),e=function(){return'<p style="margin:0;">width: '+i(window).width()+" height: "+i(window).height()+"</p>"},n.html(e()),i("body").prepend(n),i(window).resize(function(){n.html(e())})},(i=window.jQuery)?(i=window.jQuery,n()):(e=document.createElement("script"),e.src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js",e.onload=n,document.body.appendChild(e))}()}).call(this);
Original Code
The original code is in coffee:
(->
addWindowSize = ()->
style = 'background-color:azure; padding:1rem; position:fixed; right: 0; z-index:9999; font-size: 1.2rem;'
$windowSize = $('<div style="' + style + '"></div>')
getWindowSize = ->
'<p style="margin:0;">width: ' + $(window).width() + ' height: ' + $(window).height() + '</p>'
$windowSize.html getWindowSize()
$('body').prepend $windowSize
$(window).resize ->
$windowSize.html getWindowSize()
return
if !($ = window.jQuery)
# typeof jQuery=='undefined' works too
script = document.createElement('script')
script.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js'
script.onload = addWindowSize
document.body.appendChild script
else
$ = window.jQuery
addWindowSize()
)()
Basically the code is prepending a small div which updates when you resize your window.
I wrote a small javascript bookmarklet you can use to display the size. You can easily add it to your browser and whenever you click it you will see the size in the right corner of your browser window.
Here you find information how to use a bookmarklet
https://en.wikipedia.org/wiki/Bookmarklet
Bookmarklet
javascript:(function(){!function(){var i,n,e;return n=function(){var n,e,t;return t="background-color:azure; padding:1rem; position:fixed; right: 0; z-index:9999; font-size: 1.2rem;",n=i('<div style="'+t+'"></div>'),e=function(){return'<p style="margin:0;">width: '+i(window).width()+" height: "+i(window).height()+"</p>"},n.html(e()),i("body").prepend(n),i(window).resize(function(){n.html(e())})},(i=window.jQuery)?(i=window.jQuery,n()):(e=document.createElement("script"),e.src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js",e.onload=n,document.body.appendChild(e))}()}).call(this);
Original Code
The original code is in coffee:
(->
addWindowSize = ()->
style = 'background-color:azure; padding:1rem; position:fixed; right: 0; z-index:9999; font-size: 1.2rem;'
$windowSize = $('<div style="' + style + '"></div>')
getWindowSize = ->
'<p style="margin:0;">width: ' + $(window).width() + ' height: ' + $(window).height() + '</p>'
$windowSize.html getWindowSize()
$('body').prepend $windowSize
$(window).resize ->
$windowSize.html getWindowSize()
return
if !($ = window.jQuery)
# typeof jQuery=='undefined' works too
script = document.createElement('script')
script.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js'
script.onload = addWindowSize
document.body.appendChild script
else
$ = window.jQuery
addWindowSize()
)()
Basically the code is prepending a small div which updates when you resize your window.
edited Mar 31 '16 at 9:33
answered Mar 31 '16 at 9:25
Andi GigaAndi Giga
1,39821941
1,39821941
add a comment |
add a comment |
In some cases related with responsive layout $(document).height() can return wrong data that displays view port height only.
For example when some div#wrapper has height:100%, that #wrapper can be stretched by some block inside it. But it's height still will be like viewport height. In such situation you might use
$('#wrapper').get(0).scrollHeight
That represents actual size of wrapper.
So many answer with hundreds of upvotes but only this one was useful.
– Nakilon
Feb 12 '18 at 8:06
add a comment |
In some cases related with responsive layout $(document).height() can return wrong data that displays view port height only.
For example when some div#wrapper has height:100%, that #wrapper can be stretched by some block inside it. But it's height still will be like viewport height. In such situation you might use
$('#wrapper').get(0).scrollHeight
That represents actual size of wrapper.
So many answer with hundreds of upvotes but only this one was useful.
– Nakilon
Feb 12 '18 at 8:06
add a comment |
In some cases related with responsive layout $(document).height() can return wrong data that displays view port height only.
For example when some div#wrapper has height:100%, that #wrapper can be stretched by some block inside it. But it's height still will be like viewport height. In such situation you might use
$('#wrapper').get(0).scrollHeight
That represents actual size of wrapper.
In some cases related with responsive layout $(document).height() can return wrong data that displays view port height only.
For example when some div#wrapper has height:100%, that #wrapper can be stretched by some block inside it. But it's height still will be like viewport height. In such situation you might use
$('#wrapper').get(0).scrollHeight
That represents actual size of wrapper.
answered Oct 15 '15 at 8:30
Akim KelarAkim Kelar
480715
480715
So many answer with hundreds of upvotes but only this one was useful.
– Nakilon
Feb 12 '18 at 8:06
add a comment |
So many answer with hundreds of upvotes but only this one was useful.
– Nakilon
Feb 12 '18 at 8:06
So many answer with hundreds of upvotes but only this one was useful.
– Nakilon
Feb 12 '18 at 8:06
So many answer with hundreds of upvotes but only this one was useful.
– Nakilon
Feb 12 '18 at 8:06
add a comment |
I developed a library for knowing the real viewport size for desktops and mobiles browsers, because viewport sizes are inconsistents across devices and cannot rely on all the answers of that post (according to all the research I made about this) : https://github.com/pyrsmk/W
add a comment |
I developed a library for knowing the real viewport size for desktops and mobiles browsers, because viewport sizes are inconsistents across devices and cannot rely on all the answers of that post (according to all the research I made about this) : https://github.com/pyrsmk/W
add a comment |
I developed a library for knowing the real viewport size for desktops and mobiles browsers, because viewport sizes are inconsistents across devices and cannot rely on all the answers of that post (according to all the research I made about this) : https://github.com/pyrsmk/W
I developed a library for knowing the real viewport size for desktops and mobiles browsers, because viewport sizes are inconsistents across devices and cannot rely on all the answers of that post (according to all the research I made about this) : https://github.com/pyrsmk/W
answered Apr 13 '16 at 8:16
pyrsmkpyrsmk
298210
298210
add a comment |
add a comment |
Sometimes you need to see the width/height changes while resizing the window and inner content.
For that I've written a little script that adds a log box that dynamicly monitors all the resizing and almost immediatly updates.
It adds a valid HTML with fixed position and high z-index, but is small enough, so you can:
- use it on an actual site
- use it for testing mobile/responsive
views
Tested on: Chrome 40, IE11, but it is highly possible to work on other/older browsers too ... :)
function gebID(id){ return document.getElementById(id); }
function gebTN(tagName, parentEl){
if( typeof parentEl == "undefined" ) var parentEl = document;
return parentEl.getElementsByTagName(tagName);
}
function setStyleToTags(parentEl, tagName, styleString){
var tags = gebTN(tagName, parentEl);
for( var i = 0; i<tags.length; i++ ) tags[i].setAttribute('style', styleString);
}
function testSizes(){
gebID( 'screen.Width' ).innerHTML = screen.width;
gebID( 'screen.Height' ).innerHTML = screen.height;
gebID( 'window.Width' ).innerHTML = window.innerWidth;
gebID( 'window.Height' ).innerHTML = window.innerHeight;
gebID( 'documentElement.Width' ).innerHTML = document.documentElement.clientWidth;
gebID( 'documentElement.Height' ).innerHTML = document.documentElement.clientHeight;
gebID( 'body.Width' ).innerHTML = gebTN("body")[0].clientWidth;
gebID( 'body.Height' ).innerHTML = gebTN("body")[0].clientHeight;
}
var table = document.createElement('table');
table.innerHTML =
"<tr><th>SOURCE</th><th>WIDTH</th><th>x</th><th>HEIGHT</th></tr>"
+"<tr><td>screen</td><td id='screen.Width' /><td>x</td><td id='screen.Height' /></tr>"
+"<tr><td>window</td><td id='window.Width' /><td>x</td><td id='window.Height' /></tr>"
+"<tr><td>document<br>.documentElement</td><td id='documentElement.Width' /><td>x</td><td id='documentElement.Height' /></tr>"
+"<tr><td>document.body</td><td id='body.Width' /><td>x</td><td id='body.Height' /></tr>"
;
gebTN("body")[0].appendChild( table );
table.setAttribute(
'style',
"border: 2px solid black !important; position: fixed !important;"
+"left: 50% !important; top: 0px !important; padding:10px !important;"
+"width: 150px !important; font-size:18px; !important"
+"white-space: pre !important; font-family: monospace !important;"
+"z-index: 9999 !important;background: white !important;"
);
setStyleToTags(table, "td", "color: black !important; border: none !important; padding: 5px !important; text-align:center !important;");
setStyleToTags(table, "th", "color: black !important; border: none !important; padding: 5px !important; text-align:center !important;");
table.style.setProperty( 'margin-left', '-'+( table.clientWidth / 2 )+'px' );
setInterval( testSizes, 200 );
EDIT: Now styles are applied only to logger table element - not to all tables - also this is a jQuery-free solution :)
add a comment |
Sometimes you need to see the width/height changes while resizing the window and inner content.
For that I've written a little script that adds a log box that dynamicly monitors all the resizing and almost immediatly updates.
It adds a valid HTML with fixed position and high z-index, but is small enough, so you can:
- use it on an actual site
- use it for testing mobile/responsive
views
Tested on: Chrome 40, IE11, but it is highly possible to work on other/older browsers too ... :)
function gebID(id){ return document.getElementById(id); }
function gebTN(tagName, parentEl){
if( typeof parentEl == "undefined" ) var parentEl = document;
return parentEl.getElementsByTagName(tagName);
}
function setStyleToTags(parentEl, tagName, styleString){
var tags = gebTN(tagName, parentEl);
for( var i = 0; i<tags.length; i++ ) tags[i].setAttribute('style', styleString);
}
function testSizes(){
gebID( 'screen.Width' ).innerHTML = screen.width;
gebID( 'screen.Height' ).innerHTML = screen.height;
gebID( 'window.Width' ).innerHTML = window.innerWidth;
gebID( 'window.Height' ).innerHTML = window.innerHeight;
gebID( 'documentElement.Width' ).innerHTML = document.documentElement.clientWidth;
gebID( 'documentElement.Height' ).innerHTML = document.documentElement.clientHeight;
gebID( 'body.Width' ).innerHTML = gebTN("body")[0].clientWidth;
gebID( 'body.Height' ).innerHTML = gebTN("body")[0].clientHeight;
}
var table = document.createElement('table');
table.innerHTML =
"<tr><th>SOURCE</th><th>WIDTH</th><th>x</th><th>HEIGHT</th></tr>"
+"<tr><td>screen</td><td id='screen.Width' /><td>x</td><td id='screen.Height' /></tr>"
+"<tr><td>window</td><td id='window.Width' /><td>x</td><td id='window.Height' /></tr>"
+"<tr><td>document<br>.documentElement</td><td id='documentElement.Width' /><td>x</td><td id='documentElement.Height' /></tr>"
+"<tr><td>document.body</td><td id='body.Width' /><td>x</td><td id='body.Height' /></tr>"
;
gebTN("body")[0].appendChild( table );
table.setAttribute(
'style',
"border: 2px solid black !important; position: fixed !important;"
+"left: 50% !important; top: 0px !important; padding:10px !important;"
+"width: 150px !important; font-size:18px; !important"
+"white-space: pre !important; font-family: monospace !important;"
+"z-index: 9999 !important;background: white !important;"
);
setStyleToTags(table, "td", "color: black !important; border: none !important; padding: 5px !important; text-align:center !important;");
setStyleToTags(table, "th", "color: black !important; border: none !important; padding: 5px !important; text-align:center !important;");
table.style.setProperty( 'margin-left', '-'+( table.clientWidth / 2 )+'px' );
setInterval( testSizes, 200 );
EDIT: Now styles are applied only to logger table element - not to all tables - also this is a jQuery-free solution :)
add a comment |
Sometimes you need to see the width/height changes while resizing the window and inner content.
For that I've written a little script that adds a log box that dynamicly monitors all the resizing and almost immediatly updates.
It adds a valid HTML with fixed position and high z-index, but is small enough, so you can:
- use it on an actual site
- use it for testing mobile/responsive
views
Tested on: Chrome 40, IE11, but it is highly possible to work on other/older browsers too ... :)
function gebID(id){ return document.getElementById(id); }
function gebTN(tagName, parentEl){
if( typeof parentEl == "undefined" ) var parentEl = document;
return parentEl.getElementsByTagName(tagName);
}
function setStyleToTags(parentEl, tagName, styleString){
var tags = gebTN(tagName, parentEl);
for( var i = 0; i<tags.length; i++ ) tags[i].setAttribute('style', styleString);
}
function testSizes(){
gebID( 'screen.Width' ).innerHTML = screen.width;
gebID( 'screen.Height' ).innerHTML = screen.height;
gebID( 'window.Width' ).innerHTML = window.innerWidth;
gebID( 'window.Height' ).innerHTML = window.innerHeight;
gebID( 'documentElement.Width' ).innerHTML = document.documentElement.clientWidth;
gebID( 'documentElement.Height' ).innerHTML = document.documentElement.clientHeight;
gebID( 'body.Width' ).innerHTML = gebTN("body")[0].clientWidth;
gebID( 'body.Height' ).innerHTML = gebTN("body")[0].clientHeight;
}
var table = document.createElement('table');
table.innerHTML =
"<tr><th>SOURCE</th><th>WIDTH</th><th>x</th><th>HEIGHT</th></tr>"
+"<tr><td>screen</td><td id='screen.Width' /><td>x</td><td id='screen.Height' /></tr>"
+"<tr><td>window</td><td id='window.Width' /><td>x</td><td id='window.Height' /></tr>"
+"<tr><td>document<br>.documentElement</td><td id='documentElement.Width' /><td>x</td><td id='documentElement.Height' /></tr>"
+"<tr><td>document.body</td><td id='body.Width' /><td>x</td><td id='body.Height' /></tr>"
;
gebTN("body")[0].appendChild( table );
table.setAttribute(
'style',
"border: 2px solid black !important; position: fixed !important;"
+"left: 50% !important; top: 0px !important; padding:10px !important;"
+"width: 150px !important; font-size:18px; !important"
+"white-space: pre !important; font-family: monospace !important;"
+"z-index: 9999 !important;background: white !important;"
);
setStyleToTags(table, "td", "color: black !important; border: none !important; padding: 5px !important; text-align:center !important;");
setStyleToTags(table, "th", "color: black !important; border: none !important; padding: 5px !important; text-align:center !important;");
table.style.setProperty( 'margin-left', '-'+( table.clientWidth / 2 )+'px' );
setInterval( testSizes, 200 );
EDIT: Now styles are applied only to logger table element - not to all tables - also this is a jQuery-free solution :)
Sometimes you need to see the width/height changes while resizing the window and inner content.
For that I've written a little script that adds a log box that dynamicly monitors all the resizing and almost immediatly updates.
It adds a valid HTML with fixed position and high z-index, but is small enough, so you can:
- use it on an actual site
- use it for testing mobile/responsive
views
Tested on: Chrome 40, IE11, but it is highly possible to work on other/older browsers too ... :)
function gebID(id){ return document.getElementById(id); }
function gebTN(tagName, parentEl){
if( typeof parentEl == "undefined" ) var parentEl = document;
return parentEl.getElementsByTagName(tagName);
}
function setStyleToTags(parentEl, tagName, styleString){
var tags = gebTN(tagName, parentEl);
for( var i = 0; i<tags.length; i++ ) tags[i].setAttribute('style', styleString);
}
function testSizes(){
gebID( 'screen.Width' ).innerHTML = screen.width;
gebID( 'screen.Height' ).innerHTML = screen.height;
gebID( 'window.Width' ).innerHTML = window.innerWidth;
gebID( 'window.Height' ).innerHTML = window.innerHeight;
gebID( 'documentElement.Width' ).innerHTML = document.documentElement.clientWidth;
gebID( 'documentElement.Height' ).innerHTML = document.documentElement.clientHeight;
gebID( 'body.Width' ).innerHTML = gebTN("body")[0].clientWidth;
gebID( 'body.Height' ).innerHTML = gebTN("body")[0].clientHeight;
}
var table = document.createElement('table');
table.innerHTML =
"<tr><th>SOURCE</th><th>WIDTH</th><th>x</th><th>HEIGHT</th></tr>"
+"<tr><td>screen</td><td id='screen.Width' /><td>x</td><td id='screen.Height' /></tr>"
+"<tr><td>window</td><td id='window.Width' /><td>x</td><td id='window.Height' /></tr>"
+"<tr><td>document<br>.documentElement</td><td id='documentElement.Width' /><td>x</td><td id='documentElement.Height' /></tr>"
+"<tr><td>document.body</td><td id='body.Width' /><td>x</td><td id='body.Height' /></tr>"
;
gebTN("body")[0].appendChild( table );
table.setAttribute(
'style',
"border: 2px solid black !important; position: fixed !important;"
+"left: 50% !important; top: 0px !important; padding:10px !important;"
+"width: 150px !important; font-size:18px; !important"
+"white-space: pre !important; font-family: monospace !important;"
+"z-index: 9999 !important;background: white !important;"
);
setStyleToTags(table, "td", "color: black !important; border: none !important; padding: 5px !important; text-align:center !important;");
setStyleToTags(table, "th", "color: black !important; border: none !important; padding: 5px !important; text-align:center !important;");
table.style.setProperty( 'margin-left', '-'+( table.clientWidth / 2 )+'px' );
setInterval( testSizes, 200 );
EDIT: Now styles are applied only to logger table element - not to all tables - also this is a jQuery-free solution :)
edited Dec 1 '15 at 12:17
answered Mar 2 '15 at 16:05
jave.webjave.web
6,80395478
6,80395478
add a comment |
add a comment |
You can use the Screen object to get this.
The following is an example of what it would return:
Screen {
availWidth: 1920,
availHeight: 1040,
width: 1920,
height: 1080,
colorDepth: 24,
pixelDepth: 24,
top: 414,
left: 1920,
availTop: 414,
availLeft: 1920
}
To get your screenWidth variable, just use screen.width, same with screenHeight, you would just use screen.height.
To get your window width and height, it would be screen.availWidth or screen.availHeight respectively.
For the pageX and pageY variables, use window.screenX or Y. Note that this is from the VERY LEFT/TOP OF YOUR LEFT/TOP-est SCREEN. So if you have two screens of width 1920 then a window 500px from the left of the right screen would have an X value of 2420 (1920+500). screen.width/height, however, display the CURRENT screen's width or height.
To get the width and height of your page, use jQuery's $(window).height() or $(window).width().
Again using jQuery, use $("html").offset().top and $("html").offset().left for your pageX and pageY values.
add a comment |
You can use the Screen object to get this.
The following is an example of what it would return:
Screen {
availWidth: 1920,
availHeight: 1040,
width: 1920,
height: 1080,
colorDepth: 24,
pixelDepth: 24,
top: 414,
left: 1920,
availTop: 414,
availLeft: 1920
}
To get your screenWidth variable, just use screen.width, same with screenHeight, you would just use screen.height.
To get your window width and height, it would be screen.availWidth or screen.availHeight respectively.
For the pageX and pageY variables, use window.screenX or Y. Note that this is from the VERY LEFT/TOP OF YOUR LEFT/TOP-est SCREEN. So if you have two screens of width 1920 then a window 500px from the left of the right screen would have an X value of 2420 (1920+500). screen.width/height, however, display the CURRENT screen's width or height.
To get the width and height of your page, use jQuery's $(window).height() or $(window).width().
Again using jQuery, use $("html").offset().top and $("html").offset().left for your pageX and pageY values.
add a comment |
You can use the Screen object to get this.
The following is an example of what it would return:
Screen {
availWidth: 1920,
availHeight: 1040,
width: 1920,
height: 1080,
colorDepth: 24,
pixelDepth: 24,
top: 414,
left: 1920,
availTop: 414,
availLeft: 1920
}
To get your screenWidth variable, just use screen.width, same with screenHeight, you would just use screen.height.
To get your window width and height, it would be screen.availWidth or screen.availHeight respectively.
For the pageX and pageY variables, use window.screenX or Y. Note that this is from the VERY LEFT/TOP OF YOUR LEFT/TOP-est SCREEN. So if you have two screens of width 1920 then a window 500px from the left of the right screen would have an X value of 2420 (1920+500). screen.width/height, however, display the CURRENT screen's width or height.
To get the width and height of your page, use jQuery's $(window).height() or $(window).width().
Again using jQuery, use $("html").offset().top and $("html").offset().left for your pageX and pageY values.
You can use the Screen object to get this.
The following is an example of what it would return:
Screen {
availWidth: 1920,
availHeight: 1040,
width: 1920,
height: 1080,
colorDepth: 24,
pixelDepth: 24,
top: 414,
left: 1920,
availTop: 414,
availLeft: 1920
}
To get your screenWidth variable, just use screen.width, same with screenHeight, you would just use screen.height.
To get your window width and height, it would be screen.availWidth or screen.availHeight respectively.
For the pageX and pageY variables, use window.screenX or Y. Note that this is from the VERY LEFT/TOP OF YOUR LEFT/TOP-est SCREEN. So if you have two screens of width 1920 then a window 500px from the left of the right screen would have an X value of 2420 (1920+500). screen.width/height, however, display the CURRENT screen's width or height.
To get the width and height of your page, use jQuery's $(window).height() or $(window).width().
Again using jQuery, use $("html").offset().top and $("html").offset().left for your pageX and pageY values.
edited Mar 11 '16 at 15:52
Endless
12.8k65471
12.8k65471
answered Nov 6 '15 at 4:47
Zach BarhamZach Barham
3821716
3821716
add a comment |
add a comment |
here is my solution!
// innerWidth
const screen_viewport_inner = () => {
let w = window,
i = `inner`;
if (!(`innerWidth` in window)) {
i = `client`;
w = document.documentElement || document.body;
}
return {
width: w[`${i}Width`],
height: w[`${i}Height`]
}
};
// outerWidth
const screen_viewport_outer = () => {
let w = window,
o = `outer`;
if (!(`outerWidth` in window)) {
o = `client`;
w = document.documentElement || document.body;
}
return {
width: w[`${o}Width`],
height: w[`${o}Height`]
}
};
// style
const console_color = `
color: rgba(0,255,0,0.7);
font-size: 1.5rem;
border: 1px solid red;
`;
// testing
const test = () => {
let i_obj = screen_viewport_inner();
console.log(`%c screen_viewport_inner = n`, console_color, JSON.stringify(i_obj, null, 4));
let o_obj = screen_viewport_outer();
console.log(`%c screen_viewport_outer = n`, console_color, JSON.stringify(o_obj, null, 4));
};
// IIFE
(() => {
test();
})();
!screen-ok
– user8202629
Sep 13 '17 at 4:47
add a comment |
here is my solution!
// innerWidth
const screen_viewport_inner = () => {
let w = window,
i = `inner`;
if (!(`innerWidth` in window)) {
i = `client`;
w = document.documentElement || document.body;
}
return {
width: w[`${i}Width`],
height: w[`${i}Height`]
}
};
// outerWidth
const screen_viewport_outer = () => {
let w = window,
o = `outer`;
if (!(`outerWidth` in window)) {
o = `client`;
w = document.documentElement || document.body;
}
return {
width: w[`${o}Width`],
height: w[`${o}Height`]
}
};
// style
const console_color = `
color: rgba(0,255,0,0.7);
font-size: 1.5rem;
border: 1px solid red;
`;
// testing
const test = () => {
let i_obj = screen_viewport_inner();
console.log(`%c screen_viewport_inner = n`, console_color, JSON.stringify(i_obj, null, 4));
let o_obj = screen_viewport_outer();
console.log(`%c screen_viewport_outer = n`, console_color, JSON.stringify(o_obj, null, 4));
};
// IIFE
(() => {
test();
})();
!screen-ok
– user8202629
Sep 13 '17 at 4:47
add a comment |
here is my solution!
// innerWidth
const screen_viewport_inner = () => {
let w = window,
i = `inner`;
if (!(`innerWidth` in window)) {
i = `client`;
w = document.documentElement || document.body;
}
return {
width: w[`${i}Width`],
height: w[`${i}Height`]
}
};
// outerWidth
const screen_viewport_outer = () => {
let w = window,
o = `outer`;
if (!(`outerWidth` in window)) {
o = `client`;
w = document.documentElement || document.body;
}
return {
width: w[`${o}Width`],
height: w[`${o}Height`]
}
};
// style
const console_color = `
color: rgba(0,255,0,0.7);
font-size: 1.5rem;
border: 1px solid red;
`;
// testing
const test = () => {
let i_obj = screen_viewport_inner();
console.log(`%c screen_viewport_inner = n`, console_color, JSON.stringify(i_obj, null, 4));
let o_obj = screen_viewport_outer();
console.log(`%c screen_viewport_outer = n`, console_color, JSON.stringify(o_obj, null, 4));
};
// IIFE
(() => {
test();
})();here is my solution!
// innerWidth
const screen_viewport_inner = () => {
let w = window,
i = `inner`;
if (!(`innerWidth` in window)) {
i = `client`;
w = document.documentElement || document.body;
}
return {
width: w[`${i}Width`],
height: w[`${i}Height`]
}
};
// outerWidth
const screen_viewport_outer = () => {
let w = window,
o = `outer`;
if (!(`outerWidth` in window)) {
o = `client`;
w = document.documentElement || document.body;
}
return {
width: w[`${o}Width`],
height: w[`${o}Height`]
}
};
// style
const console_color = `
color: rgba(0,255,0,0.7);
font-size: 1.5rem;
border: 1px solid red;
`;
// testing
const test = () => {
let i_obj = screen_viewport_inner();
console.log(`%c screen_viewport_inner = n`, console_color, JSON.stringify(i_obj, null, 4));
let o_obj = screen_viewport_outer();
console.log(`%c screen_viewport_outer = n`, console_color, JSON.stringify(o_obj, null, 4));
};
// IIFE
(() => {
test();
})();// innerWidth
const screen_viewport_inner = () => {
let w = window,
i = `inner`;
if (!(`innerWidth` in window)) {
i = `client`;
w = document.documentElement || document.body;
}
return {
width: w[`${i}Width`],
height: w[`${i}Height`]
}
};
// outerWidth
const screen_viewport_outer = () => {
let w = window,
o = `outer`;
if (!(`outerWidth` in window)) {
o = `client`;
w = document.documentElement || document.body;
}
return {
width: w[`${o}Width`],
height: w[`${o}Height`]
}
};
// style
const console_color = `
color: rgba(0,255,0,0.7);
font-size: 1.5rem;
border: 1px solid red;
`;
// testing
const test = () => {
let i_obj = screen_viewport_inner();
console.log(`%c screen_viewport_inner = n`, console_color, JSON.stringify(i_obj, null, 4));
let o_obj = screen_viewport_outer();
console.log(`%c screen_viewport_outer = n`, console_color, JSON.stringify(o_obj, null, 4));
};
// IIFE
(() => {
test();
})();// innerWidth
const screen_viewport_inner = () => {
let w = window,
i = `inner`;
if (!(`innerWidth` in window)) {
i = `client`;
w = document.documentElement || document.body;
}
return {
width: w[`${i}Width`],
height: w[`${i}Height`]
}
};
// outerWidth
const screen_viewport_outer = () => {
let w = window,
o = `outer`;
if (!(`outerWidth` in window)) {
o = `client`;
w = document.documentElement || document.body;
}
return {
width: w[`${o}Width`],
height: w[`${o}Height`]
}
};
// style
const console_color = `
color: rgba(0,255,0,0.7);
font-size: 1.5rem;
border: 1px solid red;
`;
// testing
const test = () => {
let i_obj = screen_viewport_inner();
console.log(`%c screen_viewport_inner = n`, console_color, JSON.stringify(i_obj, null, 4));
let o_obj = screen_viewport_outer();
console.log(`%c screen_viewport_outer = n`, console_color, JSON.stringify(o_obj, null, 4));
};
// IIFE
(() => {
test();
})();edited Sep 13 '17 at 4:41
answered Sep 13 '17 at 4:36
user8202629
!screen-ok
– user8202629
Sep 13 '17 at 4:47
add a comment |
!screen-ok
– user8202629
Sep 13 '17 at 4:47
!screen-ok
– user8202629
Sep 13 '17 at 4:47
!screen-ok
– user8202629
Sep 13 '17 at 4:47
add a comment |
We can now safely use alone the native javascript window api in all browsers, without the window context.
The syntax is somewhat pretty clear!
n = "<i>px</i><br>" /* separator */
dp = devicePixelRatio /* device zoom level */
body = (() => { document.body.innerHTML = /* ready! */
"Device zoom level: " + dp
+n+ "Screen width: " + screen.width
*dp+n+ "Screen height: "+ screen.height
*dp+n+ "Document frame height: " + innerHeight
*dp+n+ "Document frame width: " + innerWidth *dp+n+ "Parent document height: "+ outerHeight *dp+n+ "Parent document width: "+ outerWidth *dp+n+ "Window available height: "+ screen.availHeight *dp+n+ "Window available width: "+ screen.availWidth *dp+n+ "Document frame max scrollable X: "+ scrollMaxX *dp+n+ "Document frame max scrollable Y: "+ scrollMaxY
*dp+n+ "Distance from left screen to window: "+ screenX
*dp+n+ "Distance from top screen to window: "+ screenY
*dp+n
})()To get accurate results in every browsers and devices, results must be multiplied by the devicePixelRatio.
The Window property devicePixelRatio returns the ratio of the
resolution in physical pixels to the resolution in CSS pixels for the
current display device. This value could also be interpreted as the
ratio of pixel sizes: the size of one CSS pixel to the size of one
physical pixel. In simpler terms, this tells the browser how many of
the screen's actual pixels should be used to draw a single CSS pixel.
This is useful when dealing with the difference between rendering on a
standard display versus a HiDPI or Retina display, which use more
screen pixels to draw the same objects, resulting in a sharper image.
There is no way to be notified when this value is changed (which can
happen, for example, if the user drags the window to a display with a
different pixel density). Since there are no callbacks or events
available to detect pixel density changes, the only way to do so is to
periodically check the value of devicePixelRatio to see if it's
changed. Just don't do it too often, or you'll impact performance.
https://developer.mozilla.org/en-US/docs/Web/API/Window/devicePixelRatio
add a comment |
We can now safely use alone the native javascript window api in all browsers, without the window context.
The syntax is somewhat pretty clear!
n = "<i>px</i><br>" /* separator */
dp = devicePixelRatio /* device zoom level */
body = (() => { document.body.innerHTML = /* ready! */
"Device zoom level: " + dp
+n+ "Screen width: " + screen.width
*dp+n+ "Screen height: "+ screen.height
*dp+n+ "Document frame height: " + innerHeight
*dp+n+ "Document frame width: " + innerWidth *dp+n+ "Parent document height: "+ outerHeight *dp+n+ "Parent document width: "+ outerWidth *dp+n+ "Window available height: "+ screen.availHeight *dp+n+ "Window available width: "+ screen.availWidth *dp+n+ "Document frame max scrollable X: "+ scrollMaxX *dp+n+ "Document frame max scrollable Y: "+ scrollMaxY
*dp+n+ "Distance from left screen to window: "+ screenX
*dp+n+ "Distance from top screen to window: "+ screenY
*dp+n
})()To get accurate results in every browsers and devices, results must be multiplied by the devicePixelRatio.
The Window property devicePixelRatio returns the ratio of the
resolution in physical pixels to the resolution in CSS pixels for the
current display device. This value could also be interpreted as the
ratio of pixel sizes: the size of one CSS pixel to the size of one
physical pixel. In simpler terms, this tells the browser how many of
the screen's actual pixels should be used to draw a single CSS pixel.
This is useful when dealing with the difference between rendering on a
standard display versus a HiDPI or Retina display, which use more
screen pixels to draw the same objects, resulting in a sharper image.
There is no way to be notified when this value is changed (which can
happen, for example, if the user drags the window to a display with a
different pixel density). Since there are no callbacks or events
available to detect pixel density changes, the only way to do so is to
periodically check the value of devicePixelRatio to see if it's
changed. Just don't do it too often, or you'll impact performance.
https://developer.mozilla.org/en-US/docs/Web/API/Window/devicePixelRatio
add a comment |
We can now safely use alone the native javascript window api in all browsers, without the window context.
The syntax is somewhat pretty clear!
n = "<i>px</i><br>" /* separator */
dp = devicePixelRatio /* device zoom level */
body = (() => { document.body.innerHTML = /* ready! */
"Device zoom level: " + dp
+n+ "Screen width: " + screen.width
*dp+n+ "Screen height: "+ screen.height
*dp+n+ "Document frame height: " + innerHeight
*dp+n+ "Document frame width: " + innerWidth *dp+n+ "Parent document height: "+ outerHeight *dp+n+ "Parent document width: "+ outerWidth *dp+n+ "Window available height: "+ screen.availHeight *dp+n+ "Window available width: "+ screen.availWidth *dp+n+ "Document frame max scrollable X: "+ scrollMaxX *dp+n+ "Document frame max scrollable Y: "+ scrollMaxY
*dp+n+ "Distance from left screen to window: "+ screenX
*dp+n+ "Distance from top screen to window: "+ screenY
*dp+n
})()To get accurate results in every browsers and devices, results must be multiplied by the devicePixelRatio.
The Window property devicePixelRatio returns the ratio of the
resolution in physical pixels to the resolution in CSS pixels for the
current display device. This value could also be interpreted as the
ratio of pixel sizes: the size of one CSS pixel to the size of one
physical pixel. In simpler terms, this tells the browser how many of
the screen's actual pixels should be used to draw a single CSS pixel.
This is useful when dealing with the difference between rendering on a
standard display versus a HiDPI or Retina display, which use more
screen pixels to draw the same objects, resulting in a sharper image.
There is no way to be notified when this value is changed (which can
happen, for example, if the user drags the window to a display with a
different pixel density). Since there are no callbacks or events
available to detect pixel density changes, the only way to do so is to
periodically check the value of devicePixelRatio to see if it's
changed. Just don't do it too often, or you'll impact performance.
https://developer.mozilla.org/en-US/docs/Web/API/Window/devicePixelRatio
We can now safely use alone the native javascript window api in all browsers, without the window context.
The syntax is somewhat pretty clear!
n = "<i>px</i><br>" /* separator */
dp = devicePixelRatio /* device zoom level */
body = (() => { document.body.innerHTML = /* ready! */
"Device zoom level: " + dp
+n+ "Screen width: " + screen.width
*dp+n+ "Screen height: "+ screen.height
*dp+n+ "Document frame height: " + innerHeight
*dp+n+ "Document frame width: " + innerWidth *dp+n+ "Parent document height: "+ outerHeight *dp+n+ "Parent document width: "+ outerWidth *dp+n+ "Window available height: "+ screen.availHeight *dp+n+ "Window available width: "+ screen.availWidth *dp+n+ "Document frame max scrollable X: "+ scrollMaxX *dp+n+ "Document frame max scrollable Y: "+ scrollMaxY
*dp+n+ "Distance from left screen to window: "+ screenX
*dp+n+ "Distance from top screen to window: "+ screenY
*dp+n
})()To get accurate results in every browsers and devices, results must be multiplied by the devicePixelRatio.
The Window property devicePixelRatio returns the ratio of the
resolution in physical pixels to the resolution in CSS pixels for the
current display device. This value could also be interpreted as the
ratio of pixel sizes: the size of one CSS pixel to the size of one
physical pixel. In simpler terms, this tells the browser how many of
the screen's actual pixels should be used to draw a single CSS pixel.
This is useful when dealing with the difference between rendering on a
standard display versus a HiDPI or Retina display, which use more
screen pixels to draw the same objects, resulting in a sharper image.
There is no way to be notified when this value is changed (which can
happen, for example, if the user drags the window to a display with a
different pixel density). Since there are no callbacks or events
available to detect pixel density changes, the only way to do so is to
periodically check the value of devicePixelRatio to see if it's
changed. Just don't do it too often, or you'll impact performance.
https://developer.mozilla.org/en-US/docs/Web/API/Window/devicePixelRatio
n = "<i>px</i><br>" /* separator */
dp = devicePixelRatio /* device zoom level */
body = (() => { document.body.innerHTML = /* ready! */
"Device zoom level: " + dp
+n+ "Screen width: " + screen.width
*dp+n+ "Screen height: "+ screen.height
*dp+n+ "Document frame height: " + innerHeight
*dp+n+ "Document frame width: " + innerWidth *dp+n+ "Parent document height: "+ outerHeight *dp+n+ "Parent document width: "+ outerWidth *dp+n+ "Window available height: "+ screen.availHeight *dp+n+ "Window available width: "+ screen.availWidth *dp+n+ "Document frame max scrollable X: "+ scrollMaxX *dp+n+ "Document frame max scrollable Y: "+ scrollMaxY
*dp+n+ "Distance from left screen to window: "+ screenX
*dp+n+ "Distance from top screen to window: "+ screenY
*dp+n
})()n = "<i>px</i><br>" /* separator */
dp = devicePixelRatio /* device zoom level */
body = (() => { document.body.innerHTML = /* ready! */
"Device zoom level: " + dp
+n+ "Screen width: " + screen.width
*dp+n+ "Screen height: "+ screen.height
*dp+n+ "Document frame height: " + innerHeight
*dp+n+ "Document frame width: " + innerWidth *dp+n+ "Parent document height: "+ outerHeight *dp+n+ "Parent document width: "+ outerWidth *dp+n+ "Window available height: "+ screen.availHeight *dp+n+ "Window available width: "+ screen.availWidth *dp+n+ "Document frame max scrollable X: "+ scrollMaxX *dp+n+ "Document frame max scrollable Y: "+ scrollMaxY
*dp+n+ "Distance from left screen to window: "+ screenX
*dp+n+ "Distance from top screen to window: "+ screenY
*dp+n
})()answered Feb 3 '18 at 19:13
CryptopatCryptopat
1,80511823
1,80511823
add a comment |
add a comment |
protected by coldspeed Dec 20 '18 at 4:44
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
520
Nice pic. All questions should and answers should be like this.
– Damien Golding
Jun 12 '13 at 8:57
8
pageHeight(on a pic) u can get with: document.body.scrollHeight
– befzz
Dec 11 '13 at 20:29
3
see https://developer.mozilla.org/en-US/docs/Web/API/Window.screen and http://www.quirksmode.org/dom/w3c_cssom.html#screenview
– rawiro
May 26 '14 at 23:06
3
Interesting: ryanve.com/lab/dimensions
– Ring Ø
Jul 31 '14 at 9:59
1
Helpful tutorial -- w3schools.com/jsref/prop_win_innerheight.asp
– Uncle Iroh
Aug 1 '16 at 12:55