~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to www/media/common/util.js

  • Committer: mattgiuca
  • Date: 2008-03-05 05:55:24 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:651
util.js: Added write_cookie and read_cookie functions for JavaScript,
    allowing you to effectively treat the cookie as a dictionary
    rather than a big string.

Show diffs side-by-side

added added

removed removed

Lines of Context:
341
341
    return path;
342
342
}
343
343
 
 
344
/** Writes a JSONable object to the cookie under a particular key
 
345
 * (JSON encoded and URL encoded).
 
346
 */
 
347
function write_cookie(key, value)
 
348
{
 
349
    var sendstr = encodeURIComponent(key) + "="
 
350
        + encodeURIComponent(JSON.stringify(value));
 
351
    /* This actually just assigns to the key, not replacing the whole cookie
 
352
     * as it appears to. */
 
353
    document.cookie = sendstr;
 
354
}
 
355
/** Reads a cookie which has a JSONable object encoded as its value.
 
356
 * Returns the object, parsed from JSON.
 
357
 */
 
358
function read_cookie(key)
 
359
{
 
360
    var cookies = document.cookie.split(";");
 
361
    var checkstart = encodeURIComponent(key) + "=";
 
362
    var checklen = checkstart.length;
 
363
    for (var i=0; i<cookies.length; i++)
 
364
    {
 
365
        var cookie = cookies[i];
 
366
        while (cookie[0] == ' ')
 
367
            cookie = cookie.substr(1);
 
368
        if (cookie.substr(0, checklen) == checkstart)
 
369
        {
 
370
            var valstr = cookie.substr(checklen);
 
371
            valstr = decodeURIComponent(valstr);
 
372
            return JSON.parse(valstr);
 
373
        }
 
374
    }
 
375
}
 
376
 
344
377
/** Given an argument map, as output in the args parameter of the return of
345
378
 * parseurl, gets the first occurence of an argument in the URL string.
346
379
 * If the argument was not found, returns null.