根据客户端的显示大小在服务器端重定向


<script>
var winw = window.innerWidth;
if (winw < 640) {location.href='minw.php';}
</script>

我在页面的head标签中具有以上代码。

目的是防止在640px以下的屏幕上显示页面,并在特定页面上重定向用户,并提供有关要求的简短信息。

但是该页面仍开始加载,在一对秒后,重定向发生。

是否有可能以某种方式告诉服务器,在原始页面输出的任何输出之前,在服务器(PHP(端进行重定向的大小是什么?

我知道您要做什么,您想使用PHP进行。尽管我的家是PHP,但是如何使用客户侧样式?CSS在这里是这样的,在服务器端进行此操作意味着您必须在每个请求时发送客户端显示宽度!我一定会用" cascading 样式 sheets"而不是服务器端编程

来做样式的事情。

展开此片段以查看效果。

div#mainContent{
  display:block;
}
div#screenNote{
  display:none;
  color:red;
}
@media only screen and (max-width: 640px) {
    div#mainContent{
      display:none;
    }
    div#screenNote{
      display:block;
    }
    
}
<div id="mainContent">
<h1>This is our site home page (you have  ascreen wider than 640px)</h1>
</div>
<div id="screenNote">
  <h1>Sorry for any inconvenience, our site does not suppor responsive design yet. you can view page only with 640px display or wider</h1>
</div>

最新更新