Skip to content

Commit 0752ed1

Browse files
committed
added functions for placing many parameters in the anchor string
1 parent 241c75a commit 0752ed1

1 file changed

Lines changed: 70 additions & 2 deletions

File tree

js/zerobin.js

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,69 @@ function secondsToHuman(seconds)
2424
var v=Math.floor(seconds/(60*60*24*30)); return v+' month'+((v>1)?'s':'');
2525
}
2626

27+
/**
28+
* Converts an associative array to an encoded string
29+
* for appending to the anchor.
30+
*
31+
* @param object associative_array Object to be serialized
32+
* @return string
33+
*/
34+
function hashToParameterString(associativeArray)
35+
{
36+
var parameterString = ""
37+
for (key in associativeArray)
38+
{
39+
if( parameterString === "" )
40+
{
41+
parameterString = encodeURIComponent(key);
42+
parameterString += "=" + encodeURIComponent(associativeArray[key]);
43+
} else {
44+
parameterString += "&" + encodeURIComponent(key);
45+
parameterString += "=" + encodeURIComponent(associativeArray[key]);
46+
}
47+
}
48+
//padding for URL shorteners
49+
parameterString += "&p=p";
50+
51+
return parameterString;
52+
}
53+
54+
/**
55+
* Converts a string to an associative array.
56+
*
57+
* @param string parameter_string String containing parameters
58+
* @return object
59+
*/
60+
function parameterStringToHash(parameterString)
61+
{
62+
var parameterHash = {};
63+
var parameterArray = parameterString.split("&");
64+
for (var i = 0; i < parameterArray.length; i++) {
65+
//var currentParamterString = decodeURIComponent(parameterArray[i]);
66+
var pair = parameterArray[i].split("=");
67+
var key = decodeURIComponent(pair[0]);
68+
var value = decodeURIComponent(pair[1]);
69+
parameterHash[key] = value;
70+
}
71+
72+
return parameterHash;
73+
}
74+
75+
/**
76+
* Get an associative array of the parameters found in the anchor
77+
*
78+
* @return object
79+
**/
80+
function getParameterHash()
81+
{
82+
var hashIndex = window.location.href.indexOf("#");
83+
if (hashIndex >= 0) {
84+
return parameterStringToHash(window.location.href.substring(hashIndex + 1));
85+
} else {
86+
return {};
87+
}
88+
}
89+
2790
/**
2891
* Compress a message (deflate compression). Returns base64 encoded data.
2992
*
@@ -67,8 +130,13 @@ function zeroDecipher(key, data) {
67130
* eg. http://server.com/zero/?aaaa#bbbb --> http://server.com/zero/
68131
*/
69132
function scriptLocation() {
70-
return window.location.href.substring(0,window.location.href.length
71-
-window.location.search.length -window.location.hash.length);
133+
var scriptLocation = window.location.href.substring(0,window.location.href.length
134+
- window.location.search.length - window.location.hash.length);
135+
var hashIndex = scriptLocation.indexOf("#");
136+
if (hashIndex !== -1) {
137+
scriptLocation = scriptLocation.substring(0, hashIndex)
138+
}
139+
return scriptLocation
72140
}
73141

74142
/**

0 commit comments

Comments
 (0)