未生成 Javascript cookie

发布于 2024-12-03 02:24:47 字数 1272 浏览 1 评论 0原文

我正在尝试将 http://adaptive-images.com/ 实现到网站中。 它将依赖 JS-Cookies 来获取屏幕的分辨率,以便适应图像大小的 PHP 脚本知道要提供什么分辨率。 不幸的是,它无法设置 cookie(并回落到 480 分辨率......),我不知道为什么。它的作用很简单:

<script type="text/javascript">
document.cookie='resolution='+Math.max(screen.width,screen.height)+'; expires=; path=/';
</script>

就在文档的头部。然而,cookie 似乎永远不会被设置,我只会看到(它们应该出现在 Chrome DevTools 的资源部分,对吧?)GoogleAnalytics 创建的 cookie(然而,这似乎不是问题,因为删除 GA 会使没有区别)。

我以前从未使用过 JS cookie,但从我在网上看到的内容来看,语法似乎非常好,而且我在控制台或其他地方没有收到任何类型的错误消息,它似乎忽略了 cookie 设置操作。

长话短说:我不知道这里发生了什么?

谢谢!

编辑:也许这是 PHP 方面的问题,所以我也会添加这部分。 发生的情况如下:

首先:

$resolutions   = array(1920,1382, 992, 768, 480);

然后:

  if (is_numeric($_COOKIE['resolution'])) {
    $client_width = (int) $_COOKIE["resolution"]; 

    rsort($resolutions); 
    $resolution = $resolutions[0]; 

    foreach ($resolutions as $break_point) { 
      if ($client_width <= $break_point) {
        $resolution = $break_point;
      }
    }
  } 

然而,即使我的屏幕是 1920,脚本也会返回 480 分辨率?据我得到的代码,它应该返回 1920,所以我猜它似乎会在未设置 $_COOKIE['resolution'] 的情况下使用回退,这将返回到 480。

I am trying to implement this: http://adaptive-images.com/ into a website.
It will rely on JS-Cookies that will get the screen's resolution so the PHP script that adapts the image sizes knows what resolution to deliver.
Unfortunately it somehow fails to set the cookie (and falls back to 480 resolution...) and I have no idea why. What it does is simply:

<script type="text/javascript">
document.cookie='resolution='+Math.max(screen.width,screen.height)+'; expires=; path=/';
</script>

right in the head of the document. Yet, the cookie never seems to be set, I will only see (they should appear in the Resources Part of the Chrome DevTools, right?) the GoogleAnalytics-created ones (yet, this does not seem to be the problem as removing GA makes no difference).

I never used JS cookies before, but from what I can read on the web the syntax seems to be perfectly fine and also I do not get any kind of error messages in the console or elsewhere, it just seems to ignore the cookie setting action.

A long story short: I have no idea what's going on here?

Thanks!

EDIT: Maybe this is a PHP sided problem so I'll add this part too.
What happens is the following:

First off:

$resolutions   = array(1920,1382, 992, 768, 480);

Then:

  if (is_numeric($_COOKIE['resolution'])) {
    $client_width = (int) $_COOKIE["resolution"]; 

    rsort($resolutions); 
    $resolution = $resolutions[0]; 

    foreach ($resolutions as $break_point) { 
      if ($client_width <= $break_point) {
        $resolution = $break_point;
      }
    }
  } 

Yet, the script will aways return a 480 resolution, even though my screen is 1920? As far as I get the code it should be returning 1920 so I guess it seems to use the fallback in case $_COOKIE['resolution'] is not set, which would be going back to 480.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丿*梦醉红颜 2024-12-10 02:24:47

不设置过期时间会使您的 cookie 成为会话 cookie,因此它仅在浏览器会话的生命周期内持续(不会写入磁盘)。

除此之外,你的饼干似乎对我有用。当我在 jsFiddle 中使用该代码并在 Chrome 调试器中查看 cookie 时,我看到 cookie 字符串中包含一个用于解析的项目,并且它被标记为会话 cookie,而不是永久 cookie。

也许你可以描述一下你是如何读取cookie的?

我发现您正在客户端上设置 cookie,以便服务器在该域上请求下一页之前不会看到 cookie。服务器不会在第一页看到它。这可能是你的问题吗?

Not setting an expires time makes your cookie a session cookie so it only lasts for the lifetime of the browser session (it is not written to disk).

Other than that, your cookie seems to work for me. When I use that code in a jsFiddle and look in the Chrome debugger at the cookies, I see the cookie string with an item in it for resolution and it's marked as a session cookie, not a permanent cookie.

Perhaps you can describe how you are reading the cookie?

It occurs to me that you're setting the cookie on the client so the server will not see the cookie until the NEXT page requested on that domain. The server won't see it for the first page. Could this be your issue?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文