Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
63
rated 0 times [  65] [ 2]  / answers: 1 / hits: 27280  / 8 Years ago, sat, may 7, 2016, 12:00:00

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.



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.)



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>

More From » cookies

 Answers
4

Well you are not setting a name value pair



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


should be something like



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

[#62269] Thursday, May 5, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tyreem

Total Points: 540
Total Questions: 94
Total Answers: 90

Location: Palestine
Member since Tue, Jul 20, 2021
3 Years ago
;