Javascript 如何获得屏幕宽度,并从JS传递到PHP?

Javascript 如何获得屏幕宽度,并从JS传递到PHP?,javascript,php,Javascript,Php,我想做的是,根据屏幕大小,通过纯javascript显示动态内容 HTML 变量宽度=屏幕宽度; js.php <?php header("Content-Type: text/javascript;charset=UTF-8"); // Content Type $width=$_GET['width']; if($width>480){ ?> document.writeln('<div style="width:100%

我想做的是,根据屏幕大小,通过纯javascript显示动态内容

HTML


变量宽度=屏幕宽度;
js.php

<?php
    header("Content-Type: text/javascript;charset=UTF-8"); // Content Type

    $width=$_GET['width'];

    if($width>480){
?>
       document.writeln('<div style="width:100%;background-color:red;">LARGE SCREEN, GO WILD!</div>');
<?php
    }else{
?>
       document.writeln('<div style="width:100%;background-color:blue;">ITS A MOBILE, BE CONSERVATIVE!</div>');
<?php
    }
?>

document.writeln(“大屏幕,疯狂!”);
document.writeln(‘这是一部手机,保守一点!’);

错误的一点是在线与$\u得到,但我不知道如何得到正确的,请帮助。谢谢

/js.php?width=一些宽度,但这不起作用。您的服务器以PHP的形式提供文件,浏览器拒绝将PHP加载到脚本标记中。。。你确定那不行吗?@Hewwo这肯定行得通,但对我来说是个问题,因为我跟踪每个动态内容的浏览/点击/点击率。“媒体查询”只会添加永远不会被单击的视图。对不起,我没有在我原来的帖子中说明这一点。
<?php
    header("Content-Type: text/javascript;charset=UTF-8"); // Content Type

    $width=$_GET['width'];

    if($width>480){
?>
       document.writeln('<div style="width:100%;background-color:red;">LARGE SCREEN, GO WILD!</div>');
<?php
    }else{
?>
       document.writeln('<div style="width:100%;background-color:blue;">ITS A MOBILE, BE CONSERVATIVE!</div>');
<?php
    }
?>