#How to connect a script with a name depending on the page?
3 messages · Page 1 of 1 (latest)
Hi @worthy delta. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
Astro does not support dynamic script imports in the way you're trying to do. However, you can import scripts in Astro in a few ways:
- Importing a
.jsfile: You can move the contents of your<script>tag into its own.jsfile in your project and then import it in your Astro file. For example:
<script>
import "../scripts/menu.js";
</script>
This is described in the Astro tutorial (https://docs.astro.build/en/tutorial/3-components/4#importing-a-js-file).
- Including JavaScript files on your page: You can reference
.js/.tsfiles or external scripts in a<script>tag’ssrcattribute. For example:
<!-- relative path to script at `src/scripts/local.js` -->
<script src="../scripts/local.js"></script>
This is described in the Astro guide on client-side scripts (https://docs.astro.build/en/guides/client-side-scripts#include-javascript-files-on-your-page).