0

I have a really long (~70,000 characters) string that I want to insert into URL. I need to implement back-forward in a browser, so when the URL changes my app will react and change it's state.

This is the function I use to generate the hash code from the string:

String.prototype.hashCode = function () { var hash = 0, i, char; if (this.length == 0) return hash; var l = this.length; for (i = 0; i < l; i++) { char = this.charCodeAt(i); hash = ((hash << 5) - hash) + char; hash |= 0; // Convert to 32bit integer } return hash; }; 

But how can I get my string back from it's hash?

Edits: Is there any other way to compress such a long URL?

    3 Answers 3

    4

    You can't. A hash is a one-way function. 560,000 bits cannot be converted into 32 bits and back again.

      3

      With Magic! (Saying it's not possible)

      4
      • Is there any other way to compress such a long URL?CommentedAug 26, 2013 at 12:48
      • Can you transmit that data via POST? Then, the query string wouldn't be visible.CommentedAug 26, 2013 at 13:46
      • I don't want to transmit the data. I need to store app state in the url. So when I press back-forward my app will react, also I want to pass url to third party so when they copy-paste it, the app will preserve it's stateCommentedAug 26, 2013 at 17:45
      • Well, I guess you could hash the string (containing the full description of the state) and save the related state in a database of some kind. Then, when the hash is passed in the URL, you retrieve the state from the database and build your page with that.CommentedAug 27, 2013 at 13:34
      0

      Depending on your use case, maybe you could have used a URL shortener service to compress your URL. Look at this example: https://twitter.com/peterjaric/status/336941762838945792 (I used tinyurl to compress long data-uris).

      Direct link to tinyurl-page: http://tinyurl.com/peterjaric-page1

        Start asking to get answers

        Find the answer to your question by asking.

        Ask question

        Explore related questions

        See similar questions with these tags.