获取页面宽度Javascript-返回PHP变量
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'];
?>
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
}
我们可以在这里实现
在javascript中计算屏幕宽度并进行ajax调用以相应地呈现滑块
var width = screen.width;
if (width < 400) {
//AJAX call to render slider1
} else {
//AJAX call to render slider2
}
相关文章:
- 如何使用jquery返回php-json数组对象
- 如何从php返回php变量值以及html响应ajax
- 获取页面宽度Javascript-返回PHP变量
- Ajax表单不返回PHP数据
- 使用 ajaxForm Jquery 插件时返回 PHP 错误
- XMLHttpRequest以文本形式返回PHP和ASP
- 如果iframe内容为空,则返回PHP变量
- 使用ajax调用返回php值时出错
- 以JSON对象的形式返回PHP数据
- 返回php变量
- AJAX,返回php字符串并传递给javascript函数
- 查询嵌套的HTML标记以返回PHP中它的's属性的值
- 我如何发送多个请求相同的文件,并获得所需的响应返回php使用jquery ajax
- 试图从ajax返回php数据
- 在Javascript中返回PHP代码
- dojo.xhrGet返回PHP源代码,而不是文本响应
- 支付导航表单php支付网关和返回php(响应返回php页面)
- Javascript警报和返回php
- 为什么使用jquery调用php函数会返回php代码
- jQuery在点击按钮时返回PHP脚本