目前我正在使用以下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发布,等等)或简单的方法来做到这一点? Thx提前
答案 0 :(得分:1)
不是真的,因为你必须首先去更加明亮地去确定屏幕尺寸。
如果您只需要屏幕大小以用于后续请求,您可以执行ajax调用并将值保存在会话中(jquery示例):
$(document).ready(function() {
var w = screen.width;
var h = screen.height;
$.load('/yourPhpScriptToSaveTheSession.php?&w='+w+'&h='+h);
});
答案 1 :(得分:0)
这里没有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
的字符串