Javascript 在if语句中使用浏览器视口大小

Javascript 在if语句中使用浏览器视口大小,javascript,php,ajax,Javascript,Php,Ajax,我找到了以下javascript代码来获取当前的视口大小 //get viewport size var viewport = function(){ var viewport = new Object(); viewport.width = 0; viewport.height = 0; // the more standards compliant browsers (mozilla/netscape/opera/IE7) //use window.innerWidth and windo

我找到了以下javascript代码来获取当前的视口大小

//get viewport size
var viewport = function(){
var viewport = new Object();
viewport.width = 0;
viewport.height = 0;
// the more standards compliant browsers (mozilla/netscape/opera/IE7) 
//use window.innerWidth and window.innerHeight
if (typeof window.innerWidth != 'undefined')
{
    viewport.width = window.innerWidth,
    viewport.height = window.innerHeight
}
else if (typeof document.documentElement != 'undefined'
&& typeof document.documentElement.clientWidth !=
'undefined' && document.documentElement.clientWidth != 0)
{
    viewport.width = document.documentElement.clientWidth,
    viewport.height = document.documentElement.clientHeight
}
else
{
    viewport.width = document.getElementsByTagName('body')[0].clientWidth,
    viewport.height = document.getElementsByTagName('body')[0].clientHeight
}
return viewport;
};

var vp = viewport();
alert(vp.width + " x " + vp.height);
这很好,但我想知道是否可以运行一个php if语句,根据视图端口大小显示指定数量的项

差不多

if(vp > 480 x 700)
{
    $items = 4;
}
else
{
    $items = 2;
}
我知道vp不能以这种方式使用,但如果有人知道我可以在php中使用vp值的方法,我非常感谢您的帮助:

非常感谢
Luke

PHP在服务器上运行,JavaScript在客户端运行。为了让信息传递,你必须传递信息。这意味着发送消息

为此,您有许多不同的方法,具体取决于上下文:

在页面加载期间? 嗯,传输,即:创建一个小的HTML或PHP页面,客户端在其中运行该代码,然后JavaScript将自己重定向到一个新页面,在那里您可以将该值作为URL参数传递 页面加载后,是否不应重新加载页面? 使用最新的网络功能? 您可以尝试web套接字 保持一点保守? 您应该使用AJAX请求。 哦,还有一件事需要考虑:PHP脚本通常以请求-响应的方式运行。也就是说,在发送页面内容时不能等待用户反馈。
因此,您可能还需要一种跟踪用户的方法,例如通过会话ID。

我认为您不会在javascript和PHP之间传递变量值。你能做的很简单,就是在使用cookies之间架起一座桥梁。ie在js脚本中,当你得到视口的高度和宽度时,设置cookies,然后用PHP阅读它们,然后做任何事情。啊,这是一个好主意,会给你一个机会。非常感谢。