[英]Get Page Width Javascript - Return PHP variable
我正在尝试做一些相当简单的事情,但是我不是一个好的javascript。 我想做的是以下操作:1.通过Javascript获取页面宽度2.如果页面宽度小于400px,则将php变量设置为1
我的JavaSCRIPT非常差
//javascript
var width = $(window).width(); //get the width of the screen
if (width<400) {
$slidevar = 1;
}
然后在页面下方,我可以获取该变量并将其用于显示我的幻灯片
//PHP
if ($slidevar == 1) {
//Display Slideshow #1 PHP Function
} else {
//Display Slideshow #2 PHP Function
}
因此,这无法完成。
可以在页面加载中一次检索PHP中的页面宽度吗?
我找到了这段代码,但是如何获取功能呢?
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
var width = $(window).width();
//This is ajax code which will send width to your php page in the screenWidth variable
$.ajax({
url: "example.php", //this will be your php page
data : {screenwidth:width}
}).done(function() {
$(this).addClass("done");
});
</script>
//example.php code is here :
<?php
echo $width = $_REQUEST['screenwidth'];
?>
我们在这里可以做到的是
在javascript中计算屏幕宽度并进行ajax调用以相应地呈现滑块
var width = screen.width;
if (width < 400) {
//AJAX call to render slider1
} else {
//AJAX call to render slider2
}
jQuery的:
var width = $(window).width(); //get the width of the screen
if (width<400) {
var slidevar = 1;
$.ajax({
url:"the/php/page",
data: {'slidevar':slidevar},
type: 'get',
success: function(){
// do something
}
});
}
的PHP:
$slidevar = $_GET['slidevar'];
if ($slidevar == 1) {
//Display Slideshow #1 PHP Function
} else {
//Display Slideshow #2 PHP Function
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.