Javascript Cookie在Safari中不起作用 [英] Javascript Cookies not working in Safari

查看:465
本文介绍了Javascript Cookie在Safari中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试用Javascript实现基本的Cookie存储功能,该功能可以在大多数浏览器中正常运行,但不能在Safari(8.0.3)中使用。我将其简化为以下示例,其中每个其他浏览器都将文本更改为存储在cookie中的日期,但是Safari根本不存储cookie并给出一个空字符串(控制台)。 Safari设置为接受所有cookie。

I've been trying to implement a basic cookie storage function in Javascript, which works as intended in most browsers, but not Safari (8.0.3). I've stripped it down to the below example, where every other browser changes the text to the date that is stored in the cookie, but Safari doesn't store a cookie at all and gives out an empty string (no error messages in the console either). Safari is set to accept all cookies.

如果我在W3Schools.com的测试平台中输入代码,则该代码可在所有浏览器中使用,因此它与该域相关? (在JSFiddle中,它似乎根本不起作用,控制台抱怨myFunction没有定义。)

If I enter the code in the testbed at W3Schools.com, it works in every browser, so is it somehow related to the domain? (In JSFiddle it doesn't seem to work at all, with the console complaining that myFunction is not defined.)

我只发现了两个相同的旧问题类型,但在一种情况下,解决方案是在其中添加; path = /部分,而在另一种情况下,则使用逗号代替分号。

I've only found two older problems of the same type, but in one case the solution was adding the "; path=/" part, which is already in here, and in the other there was a comma in place of a semicolon.

<!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>


推荐答案

好了,您没有设置名称/值对

Well you are not setting a name value pair

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

这篇关于Javascript Cookie在Safari中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆