客户端显示分辨率检测

发布于 2024-12-05 18:12:37 字数 718 浏览 5 评论 0原文

目前我正在使用以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初熏 2024-12-12 18:12:37

事实并非如此,因为您必须首先往返浏览器才能确定屏幕尺寸。

如果您仅需要后续请求的屏幕尺寸,您可以执行 ajax 调用并将值保存在会话中(jquery 示例):

$(document).ready(function() {
  var w = screen.width;
  var h = screen.height;
  $.load('/yourPhpScriptToSaveTheSession.php?&w='+w+'&h='+h);
});

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):

$(document).ready(function() {
  var w = screen.width;
  var h = screen.height;
  $.load('/yourPhpScriptToSaveTheSession.php?&w='+w+'&h='+h);
});
宫墨修音 2024-12-12 18:12:37

这里没有 ajax,

if ( $_GET["setscreen"] ) $_SESSION['display_resolution'] = $_GET['screenwidth'] . 'x' . $_GET['screenheight'];
if ( $_GET["setscreen"] ) header('Location: ' . $_SERVER['PHP_SELF']);
if ( ! $_SESSION['display_resolution'] ) echo '<script type="text/javascript">window.location = "' . $_SERVER['PHP_SELF'] . '?setscreen=1&screenwidth="+screen.width+"&screenheight="+screen.height;</script>';

会话 display_resolution 包含一个类似于以下内容的字符串:1280 x 1024

here without ajax

if ( $_GET["setscreen"] ) $_SESSION['display_resolution'] = $_GET['screenwidth'] . 'x' . $_GET['screenheight'];
if ( $_GET["setscreen"] ) header('Location: ' . $_SERVER['PHP_SELF']);
if ( ! $_SESSION['display_resolution'] ) echo '<script type="text/javascript">window.location = "' . $_SERVER['PHP_SELF'] . '?setscreen=1&screenwidth="+screen.width+"&screenheight="+screen.height;</script>';

the session display_resolution contains a string similar to: 1280 x 1024

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文