dongqie5529 2014-05-11 03:26
浏览 30
已采纳

Javascript屏幕宽度来自php

i tried to do like this. i think its worthless

here my code..

<?php
$screen = '<script type="text/javascript">document.write(screen.width);</script>';
$echo $screen;
?>

this javascript code is work without php code..

<script type="text/javascript">
document.write(screen.width);
</script>

but i want to get only width from variable

i tried to like this get screen size from php.. but its not work.. someone can help me for do this one.. i want to variable for screen width.. if have another good method plz give me answer for this one.. thanks.

  • 写回答

1条回答 默认 最新

  • duandaodao6951 2014-05-11 03:49
    关注

    As mentioned in one of the comment, you need to differentiate client and server.

    Though to make things simple, if you want to get screen width on server side, you can do the following :

    Index.php

    <?php
    session_start();
    if(!isset($_GET['width']) && !isset($_SESSION['screen.width'])){
    ?>
    <html>
    <head>
    <script type="text/javascript">
    location.replace('http://example.com/?width='+screen.width);
    </script>
    </head>
    <body></body>
    </html>
    <?php
    die();
    }
    
    if(isset($_GET['width'])){
        $_SESSION['screen.width'] = (int) $_GET['width'];
    }
    
    echo "screen width : ".$_SESSION['screen.width'];
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?