Running Javascript in a HTML cell fails on first load

You are bumping against the security features.

It sounds like you are seeing about it working when you run the cell is described here:

Any output generated during an interactive session is trusted.

Then you want to see the paragraph right above that for what happens when you try to open it again later after you saved it. See here for a brief, more conversational explanation of then why it doesn’t run when re-opened.

Have you tried setting the notebook to trusted before you saved it?
I don’t know where you are running your stuff but if you can set it to be trusted before reopening you can possibly get it to auto-run, see more discussion on the security discussion page. Also there is the init_cell extension mentioned here. If you are using MyBinder where you won’t have a database backing things because it is initialized for each session, see here, and my follow the link the example in update to my response. (Direct link showing the trust setting command here.)