<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example: Simple Cookie Example</title>
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic">
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css">
<link rel="stylesheet" href="../assets/css/main.css">
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png">
<script src="../../build/yui/yui-min.js"></script>
</head>
<body>
<!--
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a>
-->
<div id="doc">
<div id="hd">
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1>
</div>
<h1>Example: Simple Cookie Example</h1>
<div class="yui3-g">
<div class="yui3-u-3-4">
<div id="main">
<div class="content"><div class="intro">
<p>This example shows basic usage of the YUI Cookie utility. The example checks the value of a cookie and then sets it to a new value.</p>
</div>
<div class="example yui3-skin-sam">
<pre id="results"></pre>
<script>
YUI().use('node', 'cookie', function (Y) {
var pre = Y.one('#results'),
log = function(str) {
pre.append(str + '<br>\n');
};
var currentValue = Y.Cookie.get("example");
log("Cookie's current value is '" + currentValue + "'");
var newValue = "yui" + Math.round(Math.random() * Math.PI * 1000);
log("Setting cookie's value to '" + newValue + "'");
Y.Cookie.set("example", newValue);
});
</script>
</div>
<h2>Description</h2>
<p>This example begins by getting the value of a cookie named "example". If this is the first time you've run
the example, the value should be <code>null</code>:
<pre class="code prettyprint">var currentValue = Y.Cookie.get("example");</pre>
<p>This value is shown in the browser console. Next, the cookie is set to a random value:</p>
<pre class="code prettyprint">var newValue = "yui" + Math.round(Math.random() * Math.PI);
Y.Cookie.set("example", newValue);</pre>
<p>When you reload the page, the value of the cookie should be the one that was just set.</p>
<p>Note: this example uses session cookies, so the value will be lost when you close the browser.</p>
<h2>Complete Example Source</h2>
<pre class="code prettyprint"><pre id="results"></pre>
<script>
YUI().use('node', 'cookie', function (Y) {
var pre = Y.one('#results'),
log = function(str) {
pre.append(str + '<br>\n');
};
var currentValue = Y.Cookie.get("example");
log("Cookie's current value is '" + currentValue + "'");
var newValue = "yui" + Math.round(Math.random() * Math.PI * 1000);
log("Setting cookie's value to '" + newValue + "'");
Y.Cookie.set("example", newValue);
});
</script></pre>
</div>
</div>
</div>
<div class="yui3-u-1-4">
<div class="sidebar">
<div class="sidebox">
<div class="hd">
<h2 class="no-toc">Examples</h2>
</div>
<div class="bd">
<ul class="examples">
<li data-description="Demonstrates basic usage of the Cookie utility for reading and writing cookies.">
<a href="cookie-simple-example.html">Simple Cookie Example</a>
</li>
<li data-description="Demonstrates using the Cookie utility to get, set and remove cookies.">
<a href="cookie-advanced-example.html">Advanced Cookie Example</a>
</li>
<li data-description="Demonstrates using the Cookie utility to get and set subcookies.">
<a href="cookie-subcookie-example.html">Subcookie Example</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>
<script>
YUI.Env.Tests = {
examples: [],
project: '../assets',
assets: '../assets/cookie',
name: 'cookie-simple-example',
title: 'Simple Cookie Example',
newWindow: '',
auto: false
};
YUI.Env.Tests.examples.push('cookie-simple-example');
YUI.Env.Tests.examples.push('cookie-advanced-example');
YUI.Env.Tests.examples.push('cookie-subcookie-example');
</script>
<script src="../assets/yui/test-runner.js"></script>
</body>
</html>