如何使用带条件的JavaScript在PHP中获得屏幕大小
在PHP中尝试从JS获取屏幕大小 但如果其他条件相同,则返回相同的值 如果您理解我的问题,请更新问题 我想尝试这个代码,而不是其他代码如何使用带条件的JavaScript在PHP中获得屏幕大小,javascript,php,Javascript,Php,在PHP中尝试从JS获取屏幕大小 但如果其他条件相同,则返回相同的值 如果您理解我的问题,请更新问题 我想尝试这个代码,而不是其他代码 $screenwidth = "<script> var mobilewidth = console.log(screen.width); if(mobilewidth < '768'){ document.write('Mobile'); }else{ document.write
$screenwidth = "<script>
var mobilewidth = console.log(screen.width);
if(mobilewidth < '768'){
document.write('Mobile');
}else{
document.write('Template');
}
</script>";
echo $screenwidth;
$screenwidth=”
var mobilewidth=console.log(screen.width);
如果(移动宽度<'768'){
文件。写入(“移动”);
}否则{
文件。编写(“模板”);
}
";
屏幕宽度;
输出=两种情况下的模板或移动
在PHP中
函数加载模板(){
$screenwidth=“document.write(screen.width);”;
如果($screenwidthconsole.log(screen.width)
将返回undefined
,那么您不应该将其分配给变量。只需执行以下操作即可
var mobilewidth = screen.width
如果您真的想登录,请使用console.log(mobilewidth)
遵循ir
顺便说一句,mobilewidth
是这个变量的一个非常容易引起误解的名称。如果您需要自己将其设置为一个变量,请将其命名为类似于currentWidth
,并对当前硬编码的768
值使用mobilewidth
最后,与数字768
相比,不是字符串'768'
PHP在服务器端执行,Javascript在客户端执行,因此,PHP将所有内容发送到浏览器,只有Javascript开始执行,它们不能用一段代码相互通信,您需要使用另一种方式,如AJAX或cookies,但也许最好的方法是尝试使用PHP检测设备。有一些库可以为您提供屏幕信息。您可以从这里开始:首先,您的JS中有错误:var mobilewidth=screen.width;console.log(mobilewidth);
而不是var mobilewidth=console.log(screen.width)
@SlavaRozhnev感谢它正在工作如何修复这个函数CurrentWidth(){$CurrentWidth=“var mobilewidth=screen.width;if(mobilewidth<768){document.write('Mobile');}else{document.write('Template');};echo$CurrentWidth;}
文件模板外的输出调用警告:包括(/app/design/fronted/Questions/default//html/footer.php):无法打开Stream。请创建一个新问题,不要在对无关问题答案的评论中提问。
var mobilewidth = screen.width