Skip to content

Conversation

@karlseguin
Copy link
Collaborator

This should load the "src.js":

const s = document.createElement('script');
document.getElementsByTagName('body')[0].appendChild(s);
s.src = "src.js"

Notice that src is set AFTER the element is added to the DOM. This PR enables the above, by
1 - skipping dynamically added scripts which don't have a src 2 - trying to load a script whenever set_src is called.

(2) is safe because the ScriptManager already prevents scripts from being processed multiple times.

Additionally, not only can the src be set after the script is added to the DOM, but onload and onerror can be set after the src:

s.src = "src.js"
s.onload = ...;
s.onerror = ...;

This PR also delays reading the onload/onerror callbacks until the script is done loading.

This behavior is seen on reddit.

This should load the "src.js":

```
const s = document.createElement('script');
document.getElementsByTagName('body')[0].appendChild(s);
s.src = "src.js"
```

Notice that src is set AFTER the element is added to the DOM. This PR enables
the above, by
1 - skipping dynamically added scripts which don't have a src
2 - trying to load a script whenever `set_src` is called.

(2) is safe because the ScriptManager already prevents scripts from being
processed multiple times.

Additionally, not only can the src be set after the script is added to the DOM,
but onload and onerror can be set after the src:

```
s.src = "src.js"
s.onload = ...;
s.onerror = ...;
```

This PR also delays reading the onload/onerror callbacks until the script is
done loading.

This behavior is seen on reddit.
@karlseguin karlseguin merged commit b8fc60d into main Aug 26, 2025
13 of 14 checks passed
@karlseguin karlseguin deleted the dynamic_script branch August 26, 2025 21:59
@github-actions github-actions bot locked and limited conversation to collaborators Aug 26, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants