From 956b82b825ca63a2222c90d95703a75d7a4dbd2e Mon Sep 17 00:00:00 2001 From: Sean McGregor Date: Sat, 28 Jul 2012 12:35:14 -0700 Subject: [PATCH] added functions for placing many parameters in the anchor string --- js/zerobin.js | 72 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 70 insertions(+), 2 deletions(-) diff --git a/js/zerobin.js b/js/zerobin.js index 545c39e..c255016 100644 --- a/js/zerobin.js +++ b/js/zerobin.js @@ -28,6 +28,69 @@ function secondsToHuman(seconds) var v=Math.floor(seconds/(60*60*24*30)); return v+' month'+((v>1)?'s':''); } +/** + * Converts an associative array to an encoded string + * for appending to the anchor. + * + * @param object associative_array Object to be serialized + * @return string + */ +function hashToParameterString(associativeArray) +{ + var parameterString = "" + for (key in associativeArray) + { + if( parameterString === "" ) + { + parameterString = encodeURIComponent(key); + parameterString += "=" + encodeURIComponent(associativeArray[key]); + } else { + parameterString += "&" + encodeURIComponent(key); + parameterString += "=" + encodeURIComponent(associativeArray[key]); + } + } + //padding for URL shorteners + parameterString += "&p=p"; + + return parameterString; +} + +/** + * Converts a string to an associative array. + * + * @param string parameter_string String containing parameters + * @return object + */ +function parameterStringToHash(parameterString) +{ + var parameterHash = {}; + var parameterArray = parameterString.split("&"); + for (var i = 0; i < parameterArray.length; i++) { + //var currentParamterString = decodeURIComponent(parameterArray[i]); + var pair = parameterArray[i].split("="); + var key = decodeURIComponent(pair[0]); + var value = decodeURIComponent(pair[1]); + parameterHash[key] = value; + } + + return parameterHash; +} + +/** + * Get an associative array of the parameters found in the anchor + * + * @return object + **/ +function getParameterHash() +{ + var hashIndex = window.location.href.indexOf("#"); + if (hashIndex >= 0) { + return parameterStringToHash(window.location.href.substring(hashIndex + 1)); + } else { + return {}; + } +} + /** * Compress a message (deflate compression). Returns base64 encoded data. * @@ -71,8 +134,13 @@ function zeroDecipher(key, data) { * eg. http://server.com/zero/?aaaa#bbbb --> http://server.com/zero/ */ function scriptLocation() { - return window.location.href.substring(0,window.location.href.length - -window.location.search.length -window.location.hash.length); + var scriptLocation = window.location.href.substring(0,window.location.href.length + - window.location.search.length - window.location.hash.length); + var hashIndex = scriptLocation.indexOf("#"); + if (hashIndex !== -1) { + scriptLocation = scriptLocation.substring(0, hashIndex) + } + return scriptLocation } /**