Documentation
dojo.cookie¶
dojo.cookie is your one stop for handling client side cookies. Its usage is very simple and the following example should make it clear
Usage¶
// dojo 1.7 (AMD)
require(["dojo/cookie"], function(cookie){
// To set a cookie
cookie(cookieName, cookieValue, cookieProps);
// To get a cookie
var cookieValue = cookie(cookieName);
});
// dojo < 1.7
// To set a cookie
dojo.cookie(cookieName, cookieValue, cookieProps);
// To get a cookie
var cookieValue = dojo.cookie(cookieName);
Example¶
Set a cookie by clicking on the button, reload the page and click the “Get Cookie” button to check whether the cookie got set.
Note: cookies must be enabled for this to work ;)
<script type="text/javascript">
dojo.require("dijit.form.Button"); // this is there only to make things look fancy
dojo.require("dojo.cookie");
setCookie = function(){
dojo.cookie("favouriteDish", "Noodles", {expires: 5});
};
getCookie = function(){
alert("The value of the cookie is: "+dojo.cookie("favouriteDish"));
};
</script>
<button data-dojo-type="dijit.form.Button" data-dojo-props="onClick:setCookie">Set Cookie</button> <button data-dojo-type="dijit.form.Button" data-dojo-props="onClick:getCookie">Get Cookie</button>
API Info¶
full API: | http://dojotoolkit.org/api/dojo/cookie |
---|
Examples¶
set a cookie with the JSON-serialized contents of an object which will expire 5 days from now:
dojo.cookie("configObj", dojo.toJson(config), { expires: 5 });
de-serialize a cookie back into a JavaScript object:
var config = dojo.fromJson(dojo.cookie("configObj"));
delete a cookie:
dojo.cookie("configObj", null, {expires: -1});