Home >Web Front-end >JS Tutorial >Why Can\'t Inline JavaScript Be Included in External Script Tags?

Why Can\'t Inline JavaScript Be Included in External Script Tags?

Susan Sarandon
Susan SarandonOriginal
2024-10-24 01:15:29980browse

Why Can't Inline JavaScript Be Included in External Script Tags?

Why Can't You Execute JavaScript in External Script Tags?

When using tags, external JavaScript files are loaded and executed. However, attempting to include JavaScript code within such tags, like:

<script src="myFile.js">
    alert( "This is a test" );
</script>

will fail without errors. This is because a script element can only load one script at a time, either from a URL or inline. Including both results in the inline script being ignored.

To execute JavaScript within a page, you need to create multiple