0

I have a JavaScript single page app (using backbone.js), where the app's state is stored as part of URL parameters. This webpage has a lot of checkboxes.

So if a user hits a URL like http://my-app.org/project/#app/load?checkboxID[4053]=1&checkboxID[4054]=1&checkboxID[4055]=1&checkboxID[4056]=1&checkboxID[4057]=1&checkboxID[4058]=1&checkboxID[4059]=1...goes on and on, various further params...

then the page configures itself (using Backbone.Router) with the given checkboxes selected. Notice the hash #app in the URL, meaning that the parameters are handled by the JavaScript app.

The problem is now that the app has now much more parameters than I previously thought of. Hence, with all checkboxes enabled, I get an URL that is about 98.000 characters long, whereas according to this answer it can only be 2.000 at most. Oops :-(

What's your advice to handle this? I thought maybe there is a smart way of "shortening"/"compressing" the URL string in JavaScript (like gzipping + base64 encoding the URL string), but even this does not shorten the URL enough to be under 2000 characters.

4

2 回答 2

2

我不认为通过 URL 传递 App 的状态是好的做法。您可以有一个适当命名空间的对象来存储状态。

var myApp = myApp || {};
myApp.state = myApp.state || {};

而当你使用 URL 来承载状态时,只需填充这个对象并使用它。

PS:这个答案可能不会让您满意,因为您正在寻找类似压缩的东西并且不想过多地更改您的代码。还是给你一个建议。

于 2013-03-22T10:58:06.160 回答
1

您不能重新配置 url 以将参数读取为数组而不是单个项目,例如

checkboxId=[4053, 4054, 4054 ...]

这将节省一些字符,您可以循环遍历所述数组中的 id。

如果“40”是一个普通字符,或者如果“4”是普通字符,您甚至可以从值中删除它并在运行脚本时添加它?

于 2013-03-22T10:49:04.987 回答