您可以使用 javascript 获取用户屏幕尺寸/分辨率吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10022584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can you get a users screen size/resolution using javascript?
提问by JasonMortonNZ
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.
有没有办法使用 javascript 获取用户屏幕尺寸/分辨率?我认为您可能无法使用 PHP 来执行此操作,因为它是服务器端,但由于 javascript 是客户端,我认为这可能是一种选择。
采纳答案by Phaedrus
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:
更多信息在这里:
回答by Harshil
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
)