Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript string compression for URL hash parameter

I'm looking to store a lot of data in a URL hash parameter without exceeding URL character limits.

Are there any conventional ways of compressing string length which could be then decoded on another page load?

I've seen LZW encoding used for similar solutions, however would special characters be valid for this use?

like image 780
Curtis Avatar asked Feb 09 '15 15:02

Curtis


People also ask

What is hash parameter in URL?

Hash parameter authentication. You can authenticate individual REST requests by adding a hash parameter to the URL. The hash parameter allows you to prepare REST requests that can be executed by unauthenticated users. Requests that contain the hash parameter ignore the credentials specified in the authentication header ...

How can I add or update a query string parameter?

set('PARAM_HERE', VALUE_HERE); history. pushState(null, '', url); This will preserve everything about the URL and only change or add the one query param. You can also use replaceState instead of pushState if you don't want it to create a new browser history entry.

How do I know if a URL has a hash?

Get hash value for the current URL Alternatively, if you need a hash value for the current window URL, you can just use window. location. hash , which returns a string containing a '#' , followed by the fragment identifier of the URL. If the URL does not have a fragment identifier, this returns an empty string, "" .


2 Answers

LZW encoding technically works; you'll just need to convert the LZW-encoded binary into URL-safe base64, so that the output doesn't contain special characters. Here's an MDN article on base64 in JavaScript; the URL-safe variant of base64 just replaces + with - and / with _. Of course, you're not likely to reduce the size of your string by much by doing this, unless the data you want to store is extremely compressible.

like image 67
Adam R. Nelson Avatar answered Oct 06 '22 10:10

Adam R. Nelson


You can look at smaz or shoco, which are designed for the compression of short strings. Most compression methods don't really get rolling until well after your URL length limit, so you need a specialized compressor for this case if you expect to get any gain. You can then encode the binary result using a scheme like Base 64 or a more efficient coding that uses all of the URI-safe characters.

like image 29
Mark Adler Avatar answered Oct 06 '22 12:10

Mark Adler