src/cm/media/js/lib/yui/yui_3.10.3/docs/cookie/cookie-subcookie-example.html
author gibus
Tue, 11 Feb 2014 12:33:25 +0100
changeset 572 93383e54e042
parent 525 89ef5ed3c48b
permissions -rw-r--r--
Font size for piwik optout iframe.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Example: Subcookie 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: Subcookie 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 how to get and set subcookies as well as using conversion functions when retrieving subcookie values.</p>
</div>

<div class="example yui3-skin-sam">
  <pre id="results"></pre>

<script>
YUI().use('cookie', 'node', function (Y) {

    var results = Y.one('#results'),
        log = function(str) {
            results.append(str + '<br>');
        };

    //set subcookie values
    Y.Cookie.setSub("example", "name", "Yahoo!");
    Y.Cookie.setSub("example", "today", (new Date()).toString());
    Y.Cookie.setSub("example", "count", Math.round(Math.random() * 30));

    //get subcookie values
    var name = Y.Cookie.getSub("example", "name");
    var today = Y.Cookie.getSub("example", "today", function (value) {
        return new Date(value);
    });
    var count = Y.Cookie.getSub("example", "count", Number);
    
    log("The subcookie 'name' is '" + name + "' (" + (typeof name) + ")");
    log("The subcookie 'today' is '" + today + "' (" + (typeof today) + ")");
    log("The subcookie 'count' is '" + count + "' (" + (typeof count) + ")");
       

});
</script>

</div>

<h2>Description</h2>

<p>The first three lines attempt to read the values stored in subcookies of the &quot;example&quot; cookie:</p>

<pre class="code prettyprint">var name = Y.Cookie.getSub(&quot;example&quot;, &quot;name&quot;);
var today = Y.Cookie.getSub(&quot;example&quot;, &quot;today&quot;, function (value) {
    return new Date(value);
});
var count = Y.Cookie.getSub(&quot;example&quot;, &quot;count&quot;, Number);</pre>


<p>The &quot;name&quot; subcookie stores a string so it is retrieved without specifying a third argument.</p>

<p>The &quot;today&quot; subcookie stores a date string, which should be converted to a <code>Date</code> object upon retrieval; the third argument of <code>getSub()</code> is specified as a custom function that will convert the returned value into a <code>Date</code> object.</p>

<p>The &quot;count&quot; subcookie contains a number and is converted to an actual JavaScript number by passing in the native <code>Number</code> function.</p>

<p>If any of these subcookies don't exist, <code>getSub()</code> returns <code>null</code>. This should be the case the first time you run the example.</p>

<p>The retrieved values are shown in the browser console.</p>

<p>After that, new values are assigned to the various subcookies:</p>

<pre class="code prettyprint">Y.Cookie.setSub(&quot;example&quot;, &quot;name&quot;, &quot;Yahoo!&quot;);
Y.Cookie.setSub(&quot;example&quot;, &quot;today&quot;, (new Date()).toString());
Y.Cookie.setSub(&quot;example&quot;, &quot;count&quot;, Math.round(Math.random() * 30));</pre>


<p>The &quot;name&quot; subcookie is set to &quot;Yahoo!&quot;, the &quot;today&quot; subcookie is set to the value of a new <code>Date</code> object as a string, and the &quot;count&quot; subcookie is filled with a random number. The next time you run the example, the subcookies should have these values.</p>

<h2>Complete Example Source</h2>

<pre class="code prettyprint">&lt;pre id=&quot;results&quot;&gt;&lt;&#x2F;pre&gt;

&lt;script&gt;
YUI().use(&#x27;cookie&#x27;, &#x27;node&#x27;, function (Y) {

    var results = Y.one(&#x27;#results&#x27;),
        log = function(str) {
            results.append(str + &#x27;&lt;br&gt;&#x27;);
        };

    &#x2F;&#x2F;set subcookie values
    Y.Cookie.setSub(&quot;example&quot;, &quot;name&quot;, &quot;Yahoo!&quot;);
    Y.Cookie.setSub(&quot;example&quot;, &quot;today&quot;, (new Date()).toString());
    Y.Cookie.setSub(&quot;example&quot;, &quot;count&quot;, Math.round(Math.random() * 30));

    &#x2F;&#x2F;get subcookie values
    var name = Y.Cookie.getSub(&quot;example&quot;, &quot;name&quot;);
    var today = Y.Cookie.getSub(&quot;example&quot;, &quot;today&quot;, function (value) {
        return new Date(value);
    });
    var count = Y.Cookie.getSub(&quot;example&quot;, &quot;count&quot;, Number);
    
    log(&quot;The subcookie &#x27;name&#x27; is &#x27;&quot; + name + &quot;&#x27; (&quot; + (typeof name) + &quot;)&quot;);
    log(&quot;The subcookie &#x27;today&#x27; is &#x27;&quot; + today + &quot;&#x27; (&quot; + (typeof today) + &quot;)&quot;);
    log(&quot;The subcookie &#x27;count&#x27; is &#x27;&quot; + count + &quot;&#x27; (&quot; + (typeof count) + &quot;)&quot;);
       

});
&lt;&#x2F;script&gt;</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-subcookie-example',
    title: 'Subcookie 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>