簡體   English   中英

Javascript Cookie無法在Safari中使用

[英]Javascript Cookies not working in 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>

默認情況下,iOS Safari瀏覽器不允許使用cookie。 我們必須從safari瀏覽器啟用cookie設置

所以我們已經實現了本地存儲(java腳本概念)來克服safari瀏覽器中的cookie問題。

你可以看一下這篇精彩的文章 ,它們會向你展示創建,閱讀和刪除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...
}

那么你沒有設置名稱值對

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=/";

在所有瀏覽器中顯示/隱藏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);
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM