How do you stop Javascript from a <script> block which has been removed after an AJAX call? #993
Replies: 8 comments 9 replies
-
Test with
If you load What's the standard HTMX way to limit the action of a Thanks in advance and congrats again @1cg @bencroker @benpate for HTMX! |
Beta Was this translation helpful? Give feedback.
-
Yes @bencroker. What's the best practice in HTMX when we load a HTML fragment containing a As an example, the library unpoly seems to implement this with "compilers" in which you provide a function that does the cleaning
What's the best practice for this with HTMX? |
Beta Was this translation helpful? Give feedback.
-
Hi @josephernest , I am no official htmx voice, but I can tell you that in general, in the context of web pages that get partially replaced (by htmx or anything else), Javascript needs to get managed at the page level, not the block level. Only that will enable you to achieve page-wide Javascript behavior (making components communicate with each other, interact with behaviors triggered by elements that don't exist anymore, etc.). The way I do it is using StimulusJS controllers that apply behaviors to page parts and that I trigger using htmx events. In the case of a Maybe there is an easier way to tackle the specific issue you're facing, but the approach I'm suggesting here allowed me to handle way more complicated stuff without more than 5 minutes of work every time, because it just always works. |
Beta Was this translation helpful? Give feedback.
-
@David-Guillot Thanks for these ideas! Yes very clear 👍
Here is the naive way I was thinking about: ask HTMX to launch
? How would you handle the "quit screen" cases to call |
Beta Was this translation helpful? Give feedback.
-
Hi,
|
Beta Was this translation helpful? Give feedback.
-
Hi,
Let' say I click on a
<a hx-get...
link, and HTMX does an AJAX call, then replaces a part of the DOM, for example#container
's innerHTML.Let's say this new HTML code contains a
<script>setInterval(function () { alert(); }, 5000);</script>
block. It will be executed.Then you click on another link, and
#container
is now replaced again by something else. This<script>
is no more there in the new HTML content.Question: does HTMX automatically stop the setInterval from running? If so, how?
Beta Was this translation helpful? Give feedback.
All reactions