src/cm/media/js/lib/yui/yui_3.10.3/docs/cookie/cookie-advanced-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: Advanced 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: Advanced 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 how to get, set, and remove cookies using the YUI Cookie utility.</p>
</div>

<div class="example yui3-skin-sam">
  <p>Click the buttons to interact with the cookie:</p>

<p>
<input type="button" value="Get Value" id="yui-cookie-btn1">
<input type="button" value="Set Random Value" id="yui-cookie-btn2">
<input type="button" value="Remove Value" id="yui-cookie-btn3">
</p>

<pre id="results"></pre>

<script>
YUI().use('cookie', 'node', function (Y) {
    
    var pre = Y.one('#results'),
        log = function(str) {
            pre.append(str + '\n<br>');
        }

    Y.one("#yui-cookie-btn1").on("click", function () {
        var value = Y.Cookie.get("example");
        log("Cookie 'example' has a value of '" + value + "'");
    });

    Y.one("#yui-cookie-btn2").on("click", function () {
        var newValue = "yui" + Math.round(Math.random() * Math.PI * 1000);
        Y.Cookie.set("example", newValue);
        log("Set cookie 'example' to '" + newValue + "'");
    });

    Y.one("#yui-cookie-btn3").on("click", function () {
        Y.Cookie.remove("example");
        log("Removed cookie 'example'.");
    });

});
</script>

</div>

<h2>Description</h2>

<p>This example consists of three buttons, each of which performs one of the basic cookie functions: getting a value, setting a value, and removing a value. The first button, &quot;Get Value&quot;, retrieves the value of a cookie and logs it:</p>

<pre class="code prettyprint">Y.one(&quot;#yui-cookie-btn1&quot;).on(&quot;click&quot;, function () {
    var value = Y.Cookie.get(&quot;example&quot;);
    Y.log(&quot;Cookie &#x27;example&#x27; has a value of &#x27;&quot; + value + &quot;&#x27;&quot;);
});</pre>


<p>The second button, &quot;Set Random Value&quot;, creates a random value and sets the cookie's value equal to it:</p>

<pre class="code prettyprint">Y.one(&quot;#yui-cookie-btn2&quot;).on(&quot;click&quot;, function () {
    var newValue = &quot;yui&quot; + Math.round(Math.random() * Math.PI * 1000);
    Y.Cookie.set(&quot;example&quot;, newValue);
    Y.log(&quot;Set cookie &#x27;example&#x27; to &#x27;&quot; + newValue + &quot;&#x27;&quot;);
});</pre>


<p>After clicking this button, you can go back and click &quot;Get Value&quot; to see the new value that was assigned
to the cookie (you can also check the logger output).</p>
<p>The third button, &quot;Remove Value&quot;, completely removes the cookie from the page:</p>

<pre class="code prettyprint">Y.one(&quot;#yui-cookie-btn3&quot;).on(&quot;click&quot;, function () {
    Y.Cookie.remove(&quot;example&quot;);
    Y.log(&quot;Removed cookie &#x27;example&#x27;.&quot;);
});</pre>


<p>When this button is clicked, it removes the cookie. If &quot;Get Value&quot; is clicked immediately afterwards, the value should be <code>null</code>.</p>

<h2>Complete Example Source</h2>

<pre class="code prettyprint">&lt;p&gt;Click the buttons to interact with the cookie:&lt;&#x2F;p&gt;

&lt;p&gt;
&lt;input type=&quot;button&quot; value=&quot;Get Value&quot; id=&quot;yui-cookie-btn1&quot;&gt;
&lt;input type=&quot;button&quot; value=&quot;Set Random Value&quot; id=&quot;yui-cookie-btn2&quot;&gt;
&lt;input type=&quot;button&quot; value=&quot;Remove Value&quot; id=&quot;yui-cookie-btn3&quot;&gt;
&lt;&#x2F;p&gt;

&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 pre = Y.one(&#x27;#results&#x27;),
        log = function(str) {
            pre.append(str + &#x27;\n&lt;br&gt;&#x27;);
        }

    Y.one(&quot;#yui-cookie-btn1&quot;).on(&quot;click&quot;, function () {
        var value = Y.Cookie.get(&quot;example&quot;);
        log(&quot;Cookie &#x27;example&#x27; has a value of &#x27;&quot; + value + &quot;&#x27;&quot;);
    });

    Y.one(&quot;#yui-cookie-btn2&quot;).on(&quot;click&quot;, function () {
        var newValue = &quot;yui&quot; + Math.round(Math.random() * Math.PI * 1000);
        Y.Cookie.set(&quot;example&quot;, newValue);
        log(&quot;Set cookie &#x27;example&#x27; to &#x27;&quot; + newValue + &quot;&#x27;&quot;);
    });

    Y.one(&quot;#yui-cookie-btn3&quot;).on(&quot;click&quot;, function () {
        Y.Cookie.remove(&quot;example&quot;);
        log(&quot;Removed cookie &#x27;example&#x27;.&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-advanced-example',
    title: 'Advanced 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>