bigskysoftware / bigskysoftware/htmx

Functions in Scripts Do Not Run Automatically When Script is a Module

Open
#2,650 2 comments 1 reaction 0 assignees View on GitHub
Dominant language
JavaScript
Stars
49.4k
Forks
1.7k
Avg merge
3d 22h
Merged PRs (30d)
30

Description

I am not sure if anyone else is experiencing this issue but when a script with `type="module"` is included in a swap, any function that is generally called in that script does not automatically run. I know that this behaviour is generally solved with an event listener for now, but I wonder if there is a reason for this type of behaviour.

By this I mean, if you have this code:

```

Hi there

Hi there</div>
<script src="path/to/script/test-script.js" type="module">
```

**test-script.js**
```
function testHi(){
console.log("test-hi")
}
testHi()
```

In this case, test-hi is not printed and nothing is actually printed to the console. This is despite the fact that I can see that htmx has picked up the script. Any additional voices on the best pattern for this kind of behaviour would be greatly appreciated.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.