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

**URL:** <https://forums.ankiweb.net/t/in-the-template-script-is-automatically-removed-i-cant-use-javascript/69142>\
**Category:** Help\
**Created:** [March 12, 2026, 2:35am UTC](https://forums.ankiweb.net/t/in-the-template-script-is-automatically-removed-i-cant-use-javascript/69142 "2026-03-12T02:35:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![GiHGCa](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GiHGCa](https://forums.ankiweb.net/u/GiHGCa)\
**Post date:** [March 12, 2026, 2:35am UTC](https://forums.ankiweb.net/t/in-the-template-script-is-automatically-removed-i-cant-use-javascript/69142/1 "2026-03-12T02:35:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Danika\_Dakika](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/danika_dakika/32/17815_2.png) [@Danika\_Dakika](https://forums.ankiweb.net/u/Danika_Dakika)\
**Post date:** [March 12, 2026, 5:58am UTC](https://forums.ankiweb.net/t/in-the-template-script-is-automatically-removed-i-cant-use-javascript/69142/2 "2026-03-12T05:58:15Z")

</div>

Duplicate post: [Reddit - The heart of the internet](https://www.reddit.com/r/Anki/comments/1rrhwhk/in_the_template_script_is_automatically_removed_i/)

---

<div class="post-metadata">

**Author:** ![Eltaurus](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/eltaurus/32/10287_2.png) [@Eltaurus](https://forums.ankiweb.net/u/Eltaurus)\
**Post date:** [March 12, 2026, 5:59am UTC](https://forums.ankiweb.net/t/in-the-template-script-is-automatically-removed-i-cant-use-javascript/69142/3 "2026-03-12T05:59:43Z")

</div>

> [@GiHGCa](#):
>
> whenever I put that in the template (not the field), it is automatically removed by the app

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 thing 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:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/3X/c/d/cd302979b519ab195f0dd4318b96e80bf4b08a43.png)

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?

> [@GiHGCa](#):
>
> I wish to send a screen record, but I cannot upload any attachments yet because this account is new.

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).

* * *

> [@GiHGCa](#):
>
> \<img src=“x” style=“display:none” onerror="

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.

---

<div class="post-metadata">

**Author:** ![GiHGCa](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GiHGCa](https://forums.ankiweb.net/u/GiHGCa)\
**Post date:** [March 19, 2026, 11:27pm UTC](https://forums.ankiweb.net/t/in-the-template-script-is-automatically-removed-i-cant-use-javascript/69142/4 "2026-03-19T23:27:53Z")

</div>

Thanks so much kind stranger! I am enlightened. It turns out I have mistaken the HTML field for the template.

The `onload` is helpful too! Have a nice day!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/1X/8f1279ababc5879d54e4838989f606cfe55af8c7.jpeg) [@system](https://forums.ankiweb.net/u/system)\
**Post date:** [April 18, 2026, 11:28pm UTC](https://forums.ankiweb.net/t/in-the-template-script-is-automatically-removed-i-cant-use-javascript/69142/5 "2026-04-18T23:28:20Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
