Javascript Cookie无法在Safari中使用

时间:2016-05-07 11:16:58

标签: javascript cookies safari

我一直在尝试在Javascript中实现一个基本的cookie存储功能,它在大多数浏览器中都能正常工作,但不能用于Safari(8.0.3)。我已经将其删除到下面的示例,其中每个其他浏览器将文本更改为存储在cookie中的日期,但Safari根本不存储cookie并且给出一个空字符串(没有错误消息)控制台)。 Safari已设置为接受所有Cookie。

如果我在W3Schools.com的测试平台中输入代码,它可以在每个浏览器中运行,那么它是否与域名有关? (在JSFiddle中它根本不起作用,控制台抱怨没有定义myFunction。)

我只发现了两个相同类型的旧问题,但在一个案例中,解决方案是添加“; path = /”部分,它已经在这里,而在另一个中有一个逗号代替用分号。

<!DOCTYPE html>
<html>
<body>
<p id="doesitwork" onclick="myFunction()">Does it work?</p>
<script>
function myFunction() {
    d = new Date();
    document.cookie = (d + "; expires=" + "May 31 2016 23:59:59 GMT+09:00" + "; path=/");
    var x = document.cookie;
    document.getElementById("doesitwork").innerHTML = x;
}
</script>
</body>
</html>

4 个答案:

答案 0 :(得分:2)

默认情况下,iOS Safari浏览器不允许使用Cookie。我们必须从safari浏览器启用cookie设置

所以我们已经实现了本地存储(java脚本概念)来克服safari浏览器中的cookie问题。

答案 1 :(得分:1)

您可以查看this nice article,它们会向您展示创建,读取和删除Cookie的功能,还会显示纯JS和jQuery。博客上的代码如下所示:

// Create cookie
function createCookie(name, value, days) {
    var expires;
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        expires = "; expires="+date.toGMTString();
    }
    else {
        expires = "";
    }
    document.cookie = name+"="+value+expires+"; path=/";
}

// Read cookie
function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0) === ' ') {
            c = c.substring(1,c.length);
        }
        if (c.indexOf(nameEQ) === 0) {
            return c.substring(nameEQ.length,c.length);
        }
    }
    return null;
}

// Erase cookie
function eraseCookie(name) {
    createCookie(name,"",-1);
}

像这样创建cookie:

createCookie("cookie-name", "cookie-value", 30);

像这样读取cookie:

readCookie("cookie-name");
// Usually you set it as a variable and then use it somewhere
var colorTheme = readCookie("color-theme");
// Then do some conditional crap with it
if (colorTheme == "Blue") {
    // Add a class to the body or elswere
} else {
    // Add a different class maybe...
}

答案 2 :(得分:1)

您没有设置名称值对

document.cookie = (d + "; expires=" + "May 31 2016 23:59:59 GMT+09:00" + "; path=/");

应该是

document.cookie = "time=" + d + "; expires=" + "May 31 2016 23:59:59 GMT+09:00" + "; path=/";

答案 3 :(得分:-1)

在所有浏览器(包括移动浏览器)中显示/隐藏cookie的最佳方法。

演示: http://jsfiddle.net/a4fur7k3/1/

注释: 默认情况下,消息应隐藏

  

HTML

    <div id="es_cookie_msg"  style="display:none;">
        <div class="es-cookie-msg-container">
                      <!-- Cookie Message -->
                        <span class="es-cookie-msg-text">We use cookies to help ensure our website meets your needs. By continuing to use this site you agree to our policy.
</span>   
            <!-- Close button -->
            <a id="es_cookie_close_btn" href="#" class="es-cookie-button">
                <svg class="es_cookie_close_icon" width="64" version="1.1" xmlns="http://www.w3.org/2000/svg" height="64" viewBox="0 0 64 64" xmlns:xlink="http://www.w3.org/1999/xlink" enable-background="new 0 0 64 64">
                    <g>
                    <path fill="" d="M28.941,31.786L0.613,60.114c-0.787,0.787-0.787,2.062,0,2.849c0.393,0.394,0.909,0.59,1.424,0.59   c0.516,0,1.031-0.196,1.424-0.59l28.541-28.541l28.541,28.541c0.394,0.394,0.909,0.59,1.424,0.59c0.515,0,1.031-0.196,1.424-0.59   c0.787-0.787,0.787-2.062,0-2.849L35.064,31.786L63.41,3.438c0.787-0.787,0.787-2.062,0-2.849c-0.787-0.786-2.062-0.786-2.848,0   L32.003,29.15L3.441,0.59c-0.787-0.786-2.061-0.786-2.848,0c-0.787,0.787-0.787,2.062,0,2.849L28.941,31.786z"></path>
                    </g>
                </svg>
            </a>
        </div>
    </div>
  

jQuery

jQuery( document ).ready(function() {
  jQuery('#es_cookie_close_btn').click(function() {
      jQuery('#es_cookie_msg').slideUp();
      // Set cookie
      cookieHelper.create('accepted', true);
  });
});

jQuery(function () {
  // If cookie hasnt reveioly been accepted, show banner
  if( !cookieHelper.read('accepted') ) {
      jQuery('#es_cookie_msg').slideDown();
  }
});

// Cookies set inside object
var cookieHelper = {
  // Cookies
  create: function (name, value, days) {
      if (days) {
          var date = new Date();
          date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
          var expires = "; expires=" + date.toGMTString();
      }
      else var expires = "";

      document.cookie = name + "=" + value + expires + "; path=/";
  },

  read: function(name) {
      var nameEQ = name + "=";
      var ca = document.cookie.split(';');
      for (var i = 0; i < ca.length; i++) {
          var c = ca[i];
          while (c.charAt(0) == ' ') c = c.substring(1, c.length);
          if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
      }
      return null;
  },

  erase: function(name) {
      createCookie(name, "", -1);
  }
}