Sanity is a madness put to good uses.
George Santayana (1863-1952)
Find out and display a user's screen resolution with javascript.
In javascript, the screen.width and screen.height properties contain the size a visitor's monitor is set to. Bear in mind that the size the monitor is set to is not the same as the size of the browser window a visitor is using - windows can of course be set to different sizes. To find out this information you need to check the height and width of the viewport (different methods for different browsers, unfortunately).
Your current screen resolution (if you have javascript enabled) is
To display a visitor's screen resolution, simple include the following code in your page:
<script type="text/javascript"> document.write(screen.width+'x'+screen.height); </script>
Once you know what a visitor's screen resolution is, you can redirect them to a particular page. Admittedly, you'd be better off with a page that worked at any screen resolution, but anyhow ;)
The script below will redirect users based on whether or not they have a monitor resolution of 800x600 or below:
<script type="text/javascript">
if ((screen.width<=800) && (screen.height<=600)) {
window.location.replace('http://example.com/800-600-or-less');
}
else {
window.location.replace('http://example.com/greater-than-800-600');
}
</script>
If you want to actually test different screen resolutions, use the Screen Resolution Checker
28.03.2007. 00:15
Niyaz PK said on 16.01.2008. 06:19
Thanks for the info
I used this code in my website.
JayZ said on 27.10.2008. 00:36
Screen resolution is given in pixels per meter, or similar. screen.width does not give screen resolutions.
Andy said on 27.10.2008. 19:44
JayZ - screen or display resolution is the agreed terminology to refer to the pixel dimensions of a screen. You could argue that this is not entirely accurate, but generally people refer to PPI/DPI using different terminology - that's just the way the language has evolved. Don't blame me ;)
mscro said on 18.11.2008. 21:19
This work only with main window :-( i have 2 monitors, one is 1440x900 and second 1024x768 using script and browser on second monitor(1024x768) i get resultat 1440x900 do you have idea how i can solve this problem?? tnx
anjali said on 20.11.2008. 12:35
pl. tell me argent how i can create fixable website window in any screen resolution
thx
maduranga001 said on 19.12.2008. 12:11
I did it like this.Helped me a lot.tnx
function getClientResolution() {
//document.write();
var res = $get("");
res.value = screen.width + 'x' + screen.height;
//alert(res);
}
SicleRose said on 18.03.2009. 21:39
this might be helpfull, this code gets the resolution with java then makes php variables with it:
var reswidth= (screen.width)
var resheight= (screen.height)
just make a file called: getres.php, and you can include it in any php file with:
require("getres.php");
$reswidth; is the width only,
$resheight; is the height only,
$res; is width x height.
hope it helped.
Jonathan said on 16.05.2009. 03:43
Hey, what is when I wnat only the screen resolutio:
if weidth:0-800 and height 0-600
if wiedth 801-1024 and height 601-768
how do I code this..like to ifs
arvind said on 02.06.2009. 20:11
Great info
Thanks
pelle said on 31.08.2009. 09:27
Cant believe how simple this was. Theres more to the web than php..=)
palPalani said on 27.10.2009. 11:01
excellent solution!
Harry said on 19.11.2009. 16:14
Thanks Andy,
I am thinking of re-designing some of my URLs and may incorporate some of this to offer my users a better experience.
Harry
Steve said on 05.12.2009. 14:17
What would the argument be for else if you just wanted the browser to stay on the current page?
Nico said on 02.02.2010. 09:07
Hello,
Oke, but can I change my body height according to the document.getElementsByTagName('body')[0].clientHeight?
Like to show what I mean:
Body {
Height = document.getElementsByTagName('body')[0].clientHeight;
}
And crossbrowser.
I'm trying all kind of things but it doesn't work.
auto insurance said on 27.04.2010. 02:54
Is there a way to get the window resolution as opposed to the screen resolution?
Asish said on 13.05.2010. 06:18
I understand importance of the matter but not sure how to implement it in my webpage.
Can anybody put some light?
asish@astrocopia.com
Regards
sri lanka web development company said on 15.06.2010. 13:30
Good Article, Thx
http://www.webdesigncompany.lk
Hi there,
I use javascript tot determine the screen resolution and then write a different body an d table tag at the top of the page. How do searchspiders handle this?
Regards!
Ed
Chand said on 29.06.2010. 10:50
Hi Andy, I want to know that, Is this script will work with dynamic generated content from php. Like I'm creating a blog which fetches all the posts from db and each post has anchor for comment which pops up a form for writing the comment, and close it back. I was write a very simple solution that wasn't work only work with first post. url is here. http://jsfiddle.net/5A4Vh/1/
RogerAF said on 06.07.2010. 21:16
for "auto insurance" and anyone else wishing to explore further. Screen object is a property of the Window object as described here: http://www.w3schools.com/jsref/obj_window.asp
This page is found in the javascript References: Browser objects.
Thanks, Andy -- this article helped me find out several other things as well.
paolo said on 27.08.2010. 01:54
a program that get the screen resolution using traditional JAVA.. note javascript.. please help
Yogi said on 16.09.2010. 08:18
Hey, do anybody have idea, if we can find this details from any of the http header at server-side?
Qasim Ali Khawaja said on 24.09.2010. 12:19
Is there any way we can get the size of the client browser window?
murali said on 18.10.2010. 05:10
Thank you dear sir. I am using it to offer different pages dependent on the screen resolution at my site. I am trying to build a 3D book site using VRML. A universal page is not really practical in this, and your code is an absolute legend!
Murali
zeke43 said on 30.11.2010. 04:02
Thanks Andy! I just needed a quick snippet to plug into a module on my site so that visitors would be able to know what resolution desktop wallpaper to download! Worked like a charm!
mOngsAng.gA said on 20.12.2010. 07:45
You can use jQuery to make your web page fit on any screen size by calculate the screen size minus the enough width to deduct to fit the screen size...
just like this...
$(document).ready(function(){
$("#mainContainer").css("width", screen.width - 32 + "px");
});
...but before this, you need first to link the jQuery library to your page...
...see a demo on this link:
http://tnhs.heliohost.org/home.html
Jez EMIN said on 30.04.2011. 21:07
Thank you so much - you've saved me hiring a freelancer !!
Joe Moniz said on 04.07.2011. 17:24
Hey! Thank You for posting this! I just designed and published some wallpapers on one of my sites.
Everything looked and worked great, then it dawned on me that a lot of people may not have any idea of their actual screen resolution. I did a search for some javascript code that could present it to the users and I found your site!
Excellent! Short, Sweet, and Effective. Here's what it looks (and works like) on my site http://voodoorules.com/wallpapers/VoodooRULEScomWallPaper_CoupleOnLeftHerWithTattoo.html
Thank you
Joe
Maqedoni said on 24.09.2011. 15:25
Will this script work on Internet Explrer 9 or in other new version browser such as chrome and firefox. Has any1 testes it
Trent said on 13.10.2011. 10:22
Great bit of code mate - I have a new project coming up and this is going to work the treat!
Thanks again.
rental mobil jakarta said on 14.10.2011. 07:57
Nice article, thanks for the information.
Praveena said on 22.11.2011. 06:46
How to change the visitors(clients) screen resolution according to our site resolution???
will the above code work for this??? plz reply soon.........
Amri said on 20.12.2011. 11:54
I was looking for something similar to this. I need to redirect users who use screen resolution smaller than particular size. for example, if some users access to me website from their cell phone than I will redirect them to different style that works perfectly with phone browser. Is there any way to do something like this.
thanks
Page last (manually) updated: October 14, 2011.
Questions, comments, insults or praise? Have your say: