简体   繁体   English

URL哈希参数的Javascript字符串压缩

[英]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. 我希望在URL哈希参数中存储大量数据,而不会超出URL字符限制。

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? 我已经看到LZW编码用于类似的解决方案,但是特殊字符是否适用于此用途?

LZW encoding technically works; LZW编码技术上有效; you'll just need to convert the LZW-encoded binary into URL-safe base64, so that the output doesn't contain special characters. 您只需要将LZW编码的二进制文件转换为URL安全的base64,这样输出就不会包含特殊字符。 Here's an MDN article on base64 in JavaScript ; 这是关于JavaScript中base64的MDN文章 ; the URL-safe variant of base64 just replaces + with - and / with _ . BASE64的URL安全的变体只是替换+-/_ 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. 当然,除非您要存储的数据是非常可压缩的,否则您不太可能通过这样做减少字符串的大小。

You can look at smaz or shoco , which are designed for the compression of short strings. 你可以看看smazshoco ,它们是为短弦的压缩而设计的。 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. 大多数压缩方法直到URL长度限制之后才真正开始滚动,因此如果您希望获得任何收益,则需要专门的压缩器。 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. 然后,您可以使用Base 64等方案或使用所有URI安全字符的更高效编码对二进制结果进行编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM