In PHP I have an array which I encode with JSON and then I turn into a cookie.
<?php
$array = array("name" => "bob", "age" => "20");
$arrayJSON = json_encode($array);
setcookie("name", $arrayJSON, time() + (86400 * 33), "/");
?>
I'm now trying to do the same thing with Javascript. What is the Javascript equivalent of json_encoding a Javascript array and then setting it to a cookie? Here is what I have so far:
<script>
var array = new Array();
array["name"] = "bob";
array["age"] = "20";
// Need help here
array = *javascript_json_encode*(array); // Do the Javascript equivalent of PHP's json_encode();
document.cookie = "name="+array;
</script>
I'm trying to do this so that the format of the Javascript cookie is the same format as the PHP cookie. I need the Javascript cookie format to be as follows when you enter "document.cookie" in the browser console.
I need to turn "name: bob"
and "age: 20"
into %7B%22name%22%3A%22bob%22%2C%22age%22%3A%2220%22%7D"
. Don't care how I do it.