Using HTML script tags to code while they have a source

A script element loads a single script.

It can do that from either a URL or from inline, but it still loads one script; it can’t use both types of source at the same time.

If you try to include both, the inline script will be ignored:

<script src="example.js">
    this_is_ignored();
</script>

… so you need multiple script elements.

<script src="example.js">
</script>
<script>
    this_is_NOT_ignored();
</script>

It is worth noting that the content of the script element will still exist in the DOM, so some people use it to store data in it which the JS referenced by the src will read. data-* attributes are (arguably) a cleaner approach to that though.

Leave a Comment