Get the size of the screen, current web page and browser window

1,954,097

Solution 1

You can get the size of the window or document with jQuery:

// Size of browser viewport.
$(window).height();
$(window).width();

// Size of HTML document (same as pageHeight/pageWidth in screenshot).
$(document).height();
$(document).width();

For screen size you can use the screen object:

window.screen.height;
window.screen.width;

Solution 2

This has everything you need to know: Get viewport/window size

but in short:

var win = window,
    doc = document,
    docElem = doc.documentElement,
    body = doc.getElementsByTagName('body')[0],
    x = win.innerWidth || docElem.clientWidth || body.clientWidth,
    y = win.innerHeight|| docElem.clientHeight|| body.clientHeight;
alert(x + ' × ' + y);

Fiddle

Please stop editing this answer. It's been edited 22 times now by different people to match their code format preference. It's also been pointed out that this isn't required if you only want to target modern browsers - if so you only need the following:

const width  = window.innerWidth || document.documentElement.clientWidth || 
document.body.clientWidth;
const height = window.innerHeight|| document.documentElement.clientHeight|| 
document.body.clientHeight;

console.log(width, height);

Solution 3

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;

Solution 4

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).

Solution 5

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 :)

Share:
1,954,097
turtledove
Author by

turtledove

Updated on April 02, 2021

Comments

  • turtledove
    turtledove about 3 years

    How can I get windowWidth, windowHeight, pageWidth, pageHeight, screenWidth, screenHeight, pageX, pageY, screenX, screenY which will work in all major browsers?

    screenshot describing which values are wanted

  • turtledove
    turtledove almost 14 years
    thanks, and is there any way to get pageX, pageY, screenX, screenY?
  • Chris
    Chris over 11 years
    The jQuery method height() seems to work for all elements, and returns a number (46) rather than a string like css('height') ("46px").
  • Joshua
    Joshua about 11 years
    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
  • Matt Zelenak
    Matt Zelenak over 10 years
    @turtledove sure, chain in together. Req jQuery: $(document).ready(function(){ var window_height = $(this).(window).height() + "px"; var dimensions = $("body").css("height" , window_height); var window_width = $(this).(window).width() + "px"; var dimensions = $("body").css("width" , window_width); })
  • a paid nerd
    a paid nerd over 10 years
    Why not g = document.body ?
  • Michael Mikowski
    Michael Mikowski over 10 years
    @apaidnerd: Standards defying browsers like IE8 do not support document.body. IE9, however, does.
  • Daniel W.
    Daniel W. over 10 years
    @웃웃웃웃웃 what did you edit in the answer? according to the revisions, you didn't edit anything at all
  • Ankit Jaiswal
    Ankit Jaiswal about 10 years
    @mrplants These are all in pixels.
  • Nux
    Nux almost 10 years
    @MichaelMikowski That is not true! Even IE5 supports document.body.
  • Michael Mikowski
    Michael Mikowski almost 10 years
    @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!
  • Dariux
    Dariux over 9 years
    screen.height - in android default browser gets one value, in android chrome browser get another value. I think in chrome height is divided by pixel ratio. For example if height is 800, and px ratio is 1.5 then on chrome is is 800/1.5 = 533
  • mseddon
    mseddon almost 9 years
    @MarcoKerwitz I have become fairly familiar with the jquery source. Lucky me.
  • dgo
    dgo almost 9 years
    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.
  • sigod
    sigod almost 9 years
    @DanFromGermany There was 2 new lines in the end of the file. He removed one of them.
  • Ankit Jaiswal
    Ankit Jaiswal about 8 years
    @Alberto the original question when asked was asking about a Jquery solution and has been updated several times since then.
  • Alberto Bonsanto
    Alberto Bonsanto about 8 years
    If that is the case, why does your answer, which involves jQuery, still accepted as the correct answer if it is outdated? I don't try to be silly, I just get really annoyed every time I find something like this
  • Adi Prasetyo
    Adi Prasetyo about 8 years
    could you update which explain browser viewport, html document and screen?
  • Maciej Krawczyk
    Maciej Krawczyk about 8 years
    @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.
  • Randy
    Randy almost 8 years
    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!!! :-)
  • Adam Arold
    Adam Arold over 7 years
    Stop giving jquery specific answers. The OP DID NOT ask for a jquery answer.
  • Ankit Jaiswal
    Ankit Jaiswal over 7 years
    @AdamArold Stop commenting without knowing the facts. The original question specifically asked for JQuery specific answer and since then has been edited several times by moderators. There are other answers with vanilla javascript solutions, go for them if you do not like JQuery.
  • Adam Arold
    Adam Arold over 7 years
    I know the facts. Stop assuming that I do not. The fact is that there is no jquery tag currently on the answer so your answer is out of place. Plus originally the question did not contain the jquery tag either. It was added later by someone but without need.
  • GreySage
    GreySage about 7 years
    Note that if you change your screen resolution, the returned screen width/height values will change accordingly. In other words, you are NOT getting the physical dimensions, but rather the pixel dimensions.
  • BryanGrezeszak
    BryanGrezeszak almost 7 years
    @AdamArold - Actually, as much as I dislike the fact, the original question did specifically state "or jquery" in its text. I hate jQuery too, but it's hard to flog the answerer when the OP specifically stated it as an allowable answer.
  • Admin
    Admin almost 7 years
  • IncredibleHat
    IncredibleHat over 6 years
    The OP DID ASK that jquery is an option. Whoever ninja edited the original question to exclude it is at fault here, not people giving legit answers using a worldly-accepted javascript library.
  • wybe
    wybe about 6 years
    I would just like to very quierly remark that one-letter variable names are never helpful.
  • M.Abulsoud
    M.Abulsoud over 5 years
    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.
  • Nick Steele
    Nick Steele over 4 years
    In 2020 asking how to do something with JQuery is really, really annoying... More devs do not use it than do. We can do better people. We can do better.
  • Nick Steele
    Nick Steele over 4 years
    Good point. I probably should have said getting highly rated answers involving a depreciated library that is no longer relevant all over stackoverflow in your search for an answer makes the entire platform worth less, prolongs the demise of outdated software, and wrongly indicates to new members of the community that older libraries are more relevant than they are. Stack exchange needs a depreciation feature or something.
  • Nick Steele
    Nick Steele over 4 years
    Now that I think about it... Wow. This answer is 10 years old. Before even IE11. There should be some kind of aging feature on stack exchange networks. This is going to crush the entire platform in a few years. Popular answers like this take a long time for the community to upvote something else. We need a "depreciated" tag or something
  • Sukhpreet Singh Alang
    Sukhpreet Singh Alang about 4 years
    screenX/Y is relative to the primary screen, not relative to the left/top most screen. That's why the value is negative if you're currently on a screen to the left of the primary screen.
  • WinEunuuchs2Unix
    WinEunuuchs2Unix almost 3 years
    This is the second time in two days I've seen "modern browser" in answer written in 2012. Is it safe to assume now that 2022 is around the corner the answer is for "all" browsers?
  • WinEunuuchs2Unix
    WinEunuuchs2Unix almost 3 years
    @Randy Can't an old Windows XP machine simply run a new version of Fire-Fox or whatever?
  • WinEunuuchs2Unix
    WinEunuuchs2Unix almost 3 years
    Thanks for the modern answer. I hope it gets voted further up towards the top of the page. Quick question, how do you rerun function when window is resized?
  • Randy
    Randy over 2 years
    One annoyance is that that some browsers will throw a fit (meaning it can't even be caught with a try/catch construct) if you even have a reference to non existent document or window properties. That means you just about have to detect the browser and use document.write to dynamically add code when its safe to do so. Bottom line. being compatible with all browsers back to IE-8 is getting to be a chore that even the best of us are ready to give up on.
  • Randy
    Randy over 2 years
    @WinEunuuchs2Unix Since I commented in 2016 that the answer solved my problem, why would switching browsers matter? You can't tell your site visitors to switch browsers. If the site doesn't work, they leave. And NO< you can't load the latest Firefox browser on a Windows XP machine.
  • Helge Drews
    Helge Drews over 2 years
    @WinEunuuchs2Unix You can use the resize event. See developer.mozilla.org/en-US/docs/Web/API/Window/resize_event or this Stackoverflow Post: stackoverflow.com/a/18520448/4945601
  • Shardul Birje
    Shardul Birje over 2 years
    @WinEunuuchs2Unix I think the code would work for all browsers..Since Microsoft has decided to discontinue Internet Explorer in 2022..It was/is the only browser which needed "special treatment"..
  • user3481644
    user3481644 over 2 years
    @WinEunuuchs2Unix There are many environments that cannot be elaborated on which REQUIRE specific versions of the applications being run. There are many reasons, including ease of upgrade, confidence in the version being used, the overhead of certifying a new version of the application, the cost of doing the upgrading and training the users, etc. I worked on such projects and we had to buy replacement computers on eBay because they were no longer available as new. Its the way the contract was written.