8

Is there a way to get the a users screen size/resolutions using javascript? I figured you probably can't use PHP to do this as it's server-side, but as javascript is client-side I thought it may be an option.

JasonMortonNZ
  • 3,752
  • 8
  • 34
  • 56

2 Answers2

10

Yes, you can use the following to print out the resolution for example:

<script type="text/javascript">
document.write(screen.width+'x'+screen.height);
</script>

Might not work in older browsers, but will in most recent ones.

More info here:

http://www.javascriptkit.com/howto/newtech3.shtml

Phaedrus
  • 793
  • 1
  • 10
  • 17
  • 2
    On a related note: see http://stackoverflow.com/questions/6648155/how-to-find-usable-screen-size-in-javascript if you need to find the *usable* space in the browser window. – tuomassalo Feb 11 '13 at 08:11
1

Yes i have used the following code and it works well.

var screenW = 640, screenH = 480;
if (parseInt(navigator.appVersion)>3) {
screenW = screen.width;
screenH = screen.height;
}
else if (navigator.appName == "Netscape" 
&& parseInt(navigator.appVersion)==3
&& navigator.javaEnabled()
) 
 { 
var jToolkit = java.awt.Toolkit.getDefaultToolkit();
var jScreenSize = jToolkit.getScreenSize();
screenW = jScreenSize.width;
screenH = jScreenSize.height;
}

document.write(
"Screen width = "+screenW+"<br>"
+"Screen height = "+screenH
)

Courtesy: http://www.javascripter.net/faq/screensi.htm

Harshil
  • 11
  • 1