In the template, "<script>" is automatically removed. I can't use javascript

I am using Windows desktop Anki. I am a novice in programming. I want to make a card that makes random numbers in a certain range. In anki, it turns out I need to use javascript. But most (if not all) javascript lines need <script>. But whenever I put that in the template (not the field), it is automatically removed by the app.

I looked in search engines but they all just kept on saying put <script> as if it is not being removed by the anki app. I asked A.I. (Copilot and chatGPT). Initially both told me to use <script> as well. But after telling them the problem that it is being removed automatically, they told me that it must be because <script> is being sanitized by Anki to prevent malicious codes in shared decks.

I am confused as to why this seems to be not a problem to people who use javascript in anki, when I see their code also containing <script>.

to put some context, if I put in the template:

<div> some words </div>
<script> random stuff </script>
<div> also words </div>

the moment I click outside the template, it will show:

<div> some words </div>
<div> also words </div>

chatGPT provided a workaround. This is the code it gave me:

<div>
Serum Potassium: <span id="lab"></span> mmol/L
</div>

<img src=“x” style=“display:none” onerror="
var stored=sessionStorage.getItem(‘lab’);

if(stored){
var value=parseFloat(stored);
}else{
var raw=Math.random()(6.5-2.0)+2.0;
var value=Math.round(raw10)/10;
sessionStorage.setItem(‘lab’,value);
}

document.getElementById(‘lab’).textContent=value;
">

This one work and bypasses the <script> problem. But every time the card loads in ankidroid, this shows “Card Content Error: Failed to load ‘x.bin’”, (because there is no x.bin to begin with anyway), which is not much of a problem, but annoying. This notice does not show in Anki desktop.

This problem of<script> being removed automatically is not present in Ankidroid. However, when synchronized and the card is opened in desktop, that <script> is removed.

p.s. I wish to send a screen record, but I cannot upload any attachments yet because this account is new.

Duplicate post: Reddit - The heart of the internet

It’s hard to say without the recording or description of the exact steps you are taking, but it sounds like you are trying to use field HTML editor (which is separate from the displayed field content and is opened by clicking <> next to a field) instead of the actual template editor, which is opened by clicking the Cards button and should look like this:

Scripts placed here are not supposed to be sanitized, and are allowed to perform all kinds of actions.


If that’s not the reason, can you provide more detail on how you arrive at the problem? In particular, what Note Type you use, what version of Anki do you edit it in, do you have any add-ons installed?

You can upload it somewhere else and put only a link here (essential parts of a link, if they are not fully available to you either).


That’s a nice trick. If nothing else helps, you can at least replace onerror attribute here with onload and change the src to an actual image (which will avoid the ankidroid error messages) to make this workaround fully functional.

1 Like