简体   繁体   中英

Can you get a users screen size/resolution using javascript?

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM