Does an external .js file require <script> tags?

Viewed 29356

Does an external .js file require internal and containing tags to work?

4 Answers

No, <script> tags are not needed otherwise error occurs.

For example, external.js has alert function with <script> tags.

external.js:

<script>
  alert('external')
</script>

Then, external.js is added to index.html.

index.html:

<script type="text/javascript" src="external.js"></script>

Then, error occurs. (I used google chrome)

Uncaught SyntaxError: Unexpected token '<'

So, only alert function is fine without <script> tags.

external.js:

alert('external')
Related