forked from larrybolt/online-ics-feed-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
urlhash.js
86 lines (71 loc) · 2.24 KB
/
urlhash.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* Copyright 2009 by David Kerkeslager
* Released under the BSD License (http://davidkerkeslager.com/license.txt).
*
* This library defines an object-literal which allows one to store key/value pairs after the hash (#) in the URI.
* The syntax of the storage is modeled after the way that GET variables are stored after the question mark (?) in
* the URI.
*
* Example URI: "http://www.foo.com/index.html#foo=bar&baz=quux"
*
* Note: it should be obvious that this should not be used for storing private data of any kind.
* got from: https://stackoverflow.com/a/1264046
*/
var URIHash =
{
/**
* Dump the contents of the URI hash into an associative array. If the hash is invalid, the method returns
* undefined.
*/
dump : function()
{
var hash = document.location.hash;
var dump = new Array();
if(hash.length == 0) return dump;
hash = hash.substring(1).split('&');
for(var key in hash)
{
var pair = hash[key].split('=');
if(pair.length != 2 || pair[0] in dump)
return undefined;
// escape for storage
dump[unescape(pair[0])] = unescape(pair[1]);
}
return dump;
},
/**
* Takes an associative array and stores it in the URI as a hash after the # prefix, replacing any pre-
* existing hash.
*/
load : function(array)
{
var first = true;
var hash = '';
for(var key in array)
{
if(!first) hash += '&';
hash += escape(key) + '=' + escape(array[key]);
}
document.location.hash = hash;
},
/**
* Get the value of a key from the hash. If the hash does not contain the key or the hash is invalid,
* the function returns undefined.
*/
get : function(key)
{
return this.dump()[key];
},
/**
* Set the value of a key in the hash. If the key does not exist, the key/value pair is added.
*/
set : function(key,value)
{
var dump = this.dump();
dump[key] = value;
var hash = new Array();
for(var key in dump)
hash.push(escape(key) + '=' + escape(dump[key]));
document.location.hash = hash.join('&');
}
}