JavaScript in Phoenix project

I’m trying to add JavaScript to my mix project and the html.eex page is not reading it for some reason.

I have this at the end of my .eex file:

  <script>
    document.addEventListener("DOMContentLoaded", function () {
      // Handler when the DOM is fully loaded
      alert('test')
      console.log('test')
    });
  </script>

Hello and welcome,

Putting this on my page index works as expected, maybe You can show your template?

1 Like

It’s for work so I can’t show the template :grimacing:
But it’s nothing special. Essentially just have things between

tags.

This could be easily reproduced on a new project… Just add your script on the page index and You might see it working.

1 Like

Can you tell us how you confirmed that the script is not rendered?

1 Like

OK. I tried it out the same script with the rumbl project from Chris’ Programming Phoenix book and it worked. It must be something specific to the repo I’m working with. Let me look into this more! Thank you @kokolegorille @LostKobrakai

Note that DOMContentLoaded will only fire if document.readyState is still in the loading state. By the time it is set to interactive (or later complete) the DOMContentLoaded event has already been fired.

see

2 Likes