根据屏幕分辨率包含 php 文件

Include php file according to screen resolution

本文关键字:php 文件 包含 分辨率 屏幕      更新时间:2023-09-26

首先,对不起我的英语不好...

我的网页有一些麻烦:

我想根据屏幕分辨率加载不同的样式。

我在我的页面中包含文件类型.php。

这是代码:

    <?php
if(!isset($_GET['screen_check']))
{
 echo "<script language='JavaScript'>
 <!-- 
 document.location='"$PHP_SELF?screen_check=done&Width='"+screen.width+'"&Height='"+screen.height;
 //-->
 </script>";
}
else 
{    
    if(isset($_GET['Width']) && isset($_GET['Height'])) {
 if(($_GET['Height']/$_GET['Width']) < 0.6) 
 {
 include("stylew.php");
 }
 else {
include("stylen.php");
  }
     }
     else {
     include("stylen.php");
     }
}
?> 

我的问题是,当我加载我的页面时,我有一个错误消息显示半秒钟,说:"未定义的变量PHP_SELF",这有点糟糕......但尽管如此,它还是有效的!

我想做这样的事情:

    <script>
if (screen.height / screen.width) < 0.6)
{
   <?php include stylew.php ?>
   }
   else
   {
   <?php include stylen.php ?>
   }

在不重新加载页面的情况下获取我的变量。我该怎么做?

提前谢谢你!

媒体查询将是最好的。

使用

javascript:使用 jQuery 进行宽度/高度检测的兼容性。

喜欢。。。

<script>
document.addEventListener('DOMContentLoaded', function() {
    var newStyle = document.createElement('link');
        newStyle.rel = 'stylesheet';
        newStyle.type = 'text/css';
    if ( ($(window).height() / $(window).width()) < 0.6 ) {
        newStyle.href = 'http://xxx1';
    } else {
        newStyle.href = 'http://xxx2';
    }
    document.getElementsByTagName('head')[0].appendChild(newStyle);
});
</script>