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.

Sign up to request clarification or add additional context in comments.

Comments

3

With Magic! (Saying it's not possible)

4 Comments

Is there any other way to compress such a long URL?
Can you transmit that data via POST? Then, the query string wouldn't be visible.
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 state
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.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.