htmx
htmx copied to clipboard
JavaScript code in <script> tag being run twice in Chrome and Edge, but not Firefox
If my HTML response contains a <script>
tag, like the following...
<script>
console.log('this is running');
</script>
...once the content is loaded into the DOM, the console.log()
statement will fire twice in Chrome and Edge. I've confirmed that the script tag and its contents only appear once in the DOM, but the log appears twice in the console.
The console shows that the calls are coming from two different files, named like VM1683 and VM1685 (the numbers change, but they always start with VM, and the numbers on the two files are always slightly different from each other).
This happens in both Chrome and Edge, but not in Firefox.
Any new info on this?
I've found out the Chromium based browsers evaluate the script tags if using the insertBefore()
method.
In the insertNodesBefore() function of htmx:
console.log("BEFORE INSERT BEFORE", child)
parentNode.insertBefore(child, insertBefore);
console.log("AFTER INSERT BEFORE", child)
Running on chrome gives me the output:
While running on Firefox gives me:
EDIT:
After a long time debugging I've found out that this only happens if useTemplateFragments
is set to true
.
I'd love to hear about an update on this. I just ran into it today. I need useTemplateFragements
to deal with replacing table rows + an oob swap in the same request in multiple places but also have htmx responses which return <script>
tags and not all of them can be made ok to execute twice.
Same with me, and having trouble because of the same use case of @jmichalicek
EDIT: After a long time debugging I've found out that this only happens if
useTemplateFragments
is set totrue
.
I have the same issue. I return table rows, with additional swap-oob content along for the ride. Therefore useTemplateFragments needs to be true.
All included JS also runs twice. Tolerable sometimes, but I now have a case where it isn't, and I can't workaround it.
I am having an error where when i click on a div it is clicked 2 times and in this i have added a js file to public folder and i have used ejs
Hey, do you see it still happening in htmx 1.9.11 ?
Hey, do you see it still happening in htmx 1.9.11 ?
I just upgraded to 1.9.11 and revisited this - the issue is now resolved.
Thank you very much @Telroshan - I was worried this issue has been buried, so very pleased to see the fix.