【发布时间】:2014-09-16 20:11:37
【问题描述】:
我在 Jquery 中根据屏幕宽度设置 cookie。像这样
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.js"></script>
<script>
$(document).on('pageinit','#outerPage',function(){
var windowWidth = $(this).width();
// alert(windowWidth);
if(windowWidth <290)
{
console.log('cookie:small');
$.cookie("adds", 0);
//setTestCookie("adds","0");
}
else
{
console.log('cookie :Big');
$.cookie("adds", 1);
//0setTestCookie("adds","1");
}
});
</script>
然后我使用这些 cookie 来验证 php 中的 div 以及同一个文件中的一些其他参数,就像这样
<?php
if($current_item == 2 && $_COOKIE['adds'] == '1')
{
//place the adsense after the third ad.
?>
<div id="bloque" class="addsTpl addBigphone" style="min-height:90px">
<div class="google_add">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- resultmobile -->
<ins class="adsbygoogle"
style="display:inline-block;width:320px;height:90px"
data-ad-client="ca-pub-765756"
data-ad-slot="657567"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>
</div
<?php
}
?>
到目前为止一切顺利。我面临的问题是 if() 语句仅在我重新加载页面后才变为真。
我的理解是cookie是在服务器if()语句第一次执行后设置的,因为jquery只有在服务器脚本加载完成后才会执行。
任何人都可以建议我使用任何其他方法来做到这一点。
感谢和问候
【问题讨论】:
-
php在服务器端运行,所以页面重新加载后当然可见,尝试在客户端实现你的逻辑
标签: javascript php jquery cookies jquery-cookie