【问题标题】:how to PHP if condition with browser width? [closed]如果条件与浏览器宽度有关,如何 PHP? [关闭]
【发布时间】:2014-12-19 08:41:36
【问题描述】:

我有一些类似下面的脚本,但它不起作用

<?php
$width = "<script>document.write(window.outerWidth);</script>";
$width2 = (int)$width;
echo $width2;
if ( $width2 < 800 ) { ?>
    bad
<? } else { ?>
    ok
<? } ?>

请帮帮我...

【问题讨论】:

标签: javascript html if-statement browser


【解决方案1】:

您需要 JavaScript(取自 http://stackoverflow.com/questions/4180134/how-to-get-users-screen-resolution-with-phphttp://stackoverflow.com/questions/1504459/getting-the-screen-resolution-using-php

var screenWidth = window.screen.width,
    screenHeight = window.screen.height;
You can then send it to the server via Ajax (with an XmlHttpRequest).

另请参阅MDC window.screen docs.

【讨论】:

    【解决方案2】:

    你混合了 JavaScript 和 PHP。

    <script>document.write(window.outerWidth);</script>
    

    您不能在 PHP 中执行该 JavaScript。

    如果你要获得浏览器宽度,你必须编写一个 JavaScript 并将其放入浏览器中执行。 JavaScript 应该通过 AJAX/form post 将大小发送到服务器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-06
      • 2015-02-13
      • 1970-01-01
      • 1970-01-01
      • 2017-11-11
      • 1970-01-01
      • 1970-01-01
      • 2014-11-23
      相关资源
      最近更新 更多