Skip to content
Advertisement

How to lazy load javascript libraries after a htmx request?

I’m creating a web app dashboard (Django + htmx), and I only want to load the plugins that the user needs and not load every single plugin immediately because it would slow down the site. Example: The user clicks a button, and the whole HTML body gets replaced with a Wysiwyg editor. Whats the best way to dynamically load a JS library after a htmx request?

Advertisement

Answer

If you include the script in the response it will get loaded together with the HTML.

<div>Your Plugin</div>
<script type="text/javascript" src="your_plugin.js"></script>

EDIT: To place scripts to footer for example you can use htmx swap oob https://htmx.org/attributes/hx-swap-oob/.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement