客户端显示分辨率检测
目前我正在使用以下 PHP+JavaScript 代码来检测客户端浏览器窗口大小
$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
if(isSet($_GET['w']) && isSet($_GET['h']))
{
$w = $_GET['w'];
$h = $_GET['h'];
}
if(!$w && !$h)
{
$pos= strpos($url, '?');
if($pos===false)
{
echo '<script type="text/javascript">';
echo "var w = screen.width;
var h = screen.height;
window.location.href = '".$url."?w='+w+'&h='+h;
</script>'";
}
else {
echo '<script type="text/javascript">';
echo "var w = screen.width;
var h = screen.height;
window.location.href = '".$url."&w='+w+'&h='+h;
</script>'";
}
}
我想知道,有什么方法可以优化代码(通过 ajax 发布,.. 等)或简单的方法来做到这一点?
Currently I'm using following PHP+JavaScript code to detect clients browser windows size
$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
if(isSet($_GET['w']) && isSet($_GET['h']))
{
$w = $_GET['w'];
$h = $_GET['h'];
}
if(!$w && !$h)
{
$pos= strpos($url, '?');
if($pos===false)
{
echo '<script type="text/javascript">';
echo "var w = screen.width;
var h = screen.height;
window.location.href = '".$url."?w='+w+'&h='+h;
</script>'";
}
else {
echo '<script type="text/javascript">';
echo "var w = screen.width;
var h = screen.height;
window.location.href = '".$url."&w='+w+'&h='+h;
</script>'";
}
}
I wonder, is there any way to optimize code (post via ajax, .. etc) or easy way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实并非如此,因为您必须首先往返浏览器才能确定屏幕尺寸。
如果您仅需要后续请求的屏幕尺寸,您可以执行 ajax 调用并将值保存在会话中(jquery 示例):
not really as you have to do a round trip to the broser first to determine the screensize.
If you need the screensize for subsequent request only, you could do an ajax call and save the values in the session (jquery example):
这里没有 ajax,
会话 display_resolution 包含一个类似于以下内容的字符串:1280 x 1024
here without ajax
the session display_resolution contains a string similar to: 1280 x 1024