I have checked into a few posts about this topic, and after successfully creating some random fields with a suggestion from @jcznk and now confronted with the issue of getting those fields on the card back. Works fine in Anki desktop but not Ankidroid.
After a ton of websearching, I have tried using global variables, assigning directly to the window element, assigning to the card element, and also the Anki Persistence script.
the variable, window.variable, card.variable all work on Desktop. No problem there. I did post this question on the Ankdroid github issues, and got a suggestion to try this storage.ts script. I didn’t yet attempt to complie this to js, but it also led me to try (unsuccessfully) js.storage.js, and another storage.js as well.
As I now understand, Ankidroid refreshes the webview between front and back cards. I also have the feeling that all these solutions above, persistence, storage.ts, storage.js and including html5 storage api, all use the window element to set/get the variable. (Is that correct?)
Is there any other way? Or is this just not possible on Ankidroid?
It is true that Web Storage API, such as sessionStorage, cannot be used to persist variables, but it is possible to store key/value pairs (both strings only) in sessionStorage on the question side, and then retrieve those values on the answer side. Anki-Persistence uses sessionStorage internally for clients like AnkiDroid, where global variables are not persisted.
Suppose you want to keep data between the question and answer on both Anki Desktop and AnkiDroid:
If you are using Anki 2.1.49 or earlier, I recommend using Anki-persistence.
If you are using Anki 2.1.50 or later, you can simply use sessionStorage, as sessionStorage is also available on Anki desktop.
Here is an example of using sessionStorage to display 3 random fields from 10 fields for both question and answer. ( This script does not work with 2.1.49 or earlier)
ok Thanks so much for the insight here. Actually part of my problem then was the anki persistence script on Anki 2.51. But now I have reverted to 2.49 with a crowdanki issue. So will just wait a bit till thats fixed until implementing the above. I don’t want to have something that will prevent me from updating Anki in the future. Thanks for the shuffle algorithm! I knew this would be possible, but just not there yet. I had 3 while loops that worked, but this is clearly the right way!
No, it appears not. I have been able to use sessionStorage since version 2.1.50 on Windows and Linux, even with Qt5 builds. I don’t have a Mac, so I can’t say for sure, but I would guess that it probably has nothing to do with whether the Qt version is 5 or 6. I assume that the change is probably due to the fact that the page url is now set by this commit: