From 685fdc078dd08044d3382bc803701d5c5824d5df Mon Sep 17 00:00:00 2001 From: Matthew Flatt Date: Wed, 27 Nov 2013 10:04:17 -0700 Subject: [PATCH] use localStorage for user-doc redirect Chrome doesn't allow cookies for local files, so try localStorage (and fall back to cookies where localStorage is unavailable) original commit: 2312e3b9e6feb4427f582a9a4c6132a8d67cb8f1 --- .../scribble-lib/scribble/scribble-common.js | 42 ++++++++++++------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/pkgs/scribble-pkgs/scribble-lib/scribble/scribble-common.js b/pkgs/scribble-pkgs/scribble-lib/scribble/scribble-common.js index 00eec767..75c7fcd0 100644 --- a/pkgs/scribble-pkgs/scribble-lib/scribble/scribble-common.js +++ b/pkgs/scribble-pkgs/scribble-lib/scribble/scribble-common.js @@ -53,28 +53,40 @@ function MergePageArgsIntoLink(a) { // Cookies -------------------------------------------------------------------- +// Actually, try localStorage (a la HTML 5), first. + function GetCookie(key, def) { - var i, cookiestrs; try { - if (document.cookie.length <= 0) return def; - cookiestrs = document.cookie.split(/; */); - } catch (e) { return def; } - for (i = 0; i < cookiestrs.length; i++) { - var cur = cookiestrs[i]; - var eql = cur.indexOf('='); - if (eql >= 0 && cur.substring(0,eql) == key) - return unescape(cur.substring(eql+1)); + var v = localStorage[key]; + if (!v) v = def; + return v; + } catch (e) { + var i, cookiestrs; + try { + if (document.cookie.length <= 0) return def; + cookiestrs = document.cookie.split(/; */); + } catch (e) { return def; } + for (i = 0; i < cookiestrs.length; i++) { + var cur = cookiestrs[i]; + var eql = cur.indexOf('='); + if (eql >= 0 && cur.substring(0,eql) == key) + return unescape(cur.substring(eql+1)); + } + return def; } - return def; } function SetCookie(key, val) { - var d = new Date(); - d.setTime(d.getTime()+(365*24*60*60*1000)); try { - document.cookie = - key + "=" + escape(val) + "; expires="+ d.toGMTString() + "; path=/"; - } catch (e) {} + localStorage[key] = val; + } catch(e) { + var d = new Date(); + d.setTime(d.getTime()+(365*24*60*60*1000)); + try { + document.cookie = + key + "=" + escape(val) + "; expires="+ d.toGMTString() + "; path=/"; + } catch (e) {} + } } // note that this always stores a directory name, ending with a "/"