forked from liferay/yui3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
507 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"name" : "cookie", | ||
"displayName": "Cookie Utility", | ||
"description": "Provides a simple API for interacting with cookies, including the creation and manipulation of subcookies.", | ||
"author" : "nzakas", | ||
|
||
"tags": ["utility", "cookie", "cookies"], | ||
"use" : ["cookie"], | ||
|
||
"examples": [ | ||
{ | ||
"name" : "cookie-simple-example", | ||
"displayName": "Simple Cookie Example", | ||
"description": "Demonstrates basic usage of the Cookie utility for reading and writing cookies.", | ||
"modules" : ["cookie"], | ||
"tags" : ["cookie"], | ||
|
||
"hideTableOfContents": true | ||
}, | ||
|
||
{ | ||
"name" : "cookie-advanced-example", | ||
"displayName": "Advanced Cookie Example", | ||
"description": "Demonstrates using the Cookie utility to get, set and remove cookies.", | ||
"modules" : ["cookie"], | ||
"tags" : ["cookie"], | ||
|
||
"hideTableOfContents": true | ||
}, | ||
|
||
{ | ||
"name" : "cookie-subcookie-example", | ||
"displayName": "Subcookie Example", | ||
"description": "Demonstrates using the Cookie utility to get and set subcookies.", | ||
"modules" : ["cookie"], | ||
"tags" : ["cookie"], | ||
|
||
"hideTableOfContents": true | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<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"> | ||
{{>cookie-advanced-example-source}} | ||
</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, "Get Value", retrieves the value of a cookie and displays it in an alert:</p> | ||
|
||
``` | ||
Y.one("#yui-cookie-btn1").on("click", function () { | ||
var value = Y.Cookie.get("example"); | ||
alert(value); | ||
Y.log("Cookie 'example' has a value of '" + value + "'"); | ||
}); | ||
``` | ||
|
||
<p>The second button, "Set Random Value", creates a random value and sets the cookie's value equal to it:</p> | ||
|
||
``` | ||
Y.one("#yui-cookie-btn2").on("click", function () { | ||
var newValue = "yui" + Math.round(Math.random() * Math.PI * 1000); | ||
Y.Cookie.set("example", newValue); | ||
Y.log("Set cookie 'example' to '" + newValue + "'"); | ||
}); | ||
``` | ||
|
||
<p>After clicking this button, you can go back and click "Get Value" to see the new value that was assigned | ||
to the cookie (you can also check the logger output).</p> | ||
<p>The third button, "Remove Value", completely removes the cookie from the page:</p> | ||
|
||
``` | ||
Y.one("#yui-cookie-btn3").on("click", function () { | ||
Y.Cookie.remove("example"); | ||
Y.log("Removed cookie 'example'."); | ||
}); | ||
``` | ||
|
||
<p>When this button is clicked, it removes the cookie. If "Get Value" is clicked immediately afterwards, the value should be <code>null</code>.</p> | ||
|
||
<h2>Complete Example Source</h2> | ||
|
||
``` | ||
{{>cookie-advanced-example-source}} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<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"> | ||
{{>cookie-simple-example-source}} | ||
</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>: | ||
|
||
``` | ||
var currentValue = Y.Cookie.get("example"); | ||
``` | ||
|
||
<p>This value is shown in the browser console. Next, the cookie is set to a random value:</p> | ||
|
||
``` | ||
var newValue = "yui" + Math.round(Math.random() * Math.PI); | ||
Y.Cookie.set("example", newValue); | ||
``` | ||
|
||
<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> | ||
|
||
``` | ||
{{>cookie-simple-example-source}} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<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"> | ||
{{>cookie-subcookie-example-source}} | ||
</div> | ||
|
||
<h2>Description</h2> | ||
|
||
<p>The first three lines attempt to read the values stored in subcookies of the "example" cookie:</p> | ||
|
||
``` | ||
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); | ||
``` | ||
|
||
<p>The "name" subcookie stores a string so it is retrieved without specifying a third argument.</p> | ||
|
||
<p>The "today" 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 "count" 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> | ||
|
||
``` | ||
Y.Cookie.setSub("example", "name", "Yahoo!"); | ||
Y.Cookie.setSub("example", "today", (new Date()).toString()); | ||
Y.Cookie.setSub("example", "count", Math.round(Math.random() * 30)); | ||
``` | ||
|
||
<p>The "name" subcookie is set to "Yahoo!", the "today" subcookie is set to the value of a new <code>Date</code> object as a string, and the "count" 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> | ||
|
||
``` | ||
{{>cookie-subcookie-example-source}} | ||
``` |
Oops, something went wrong.