Codewell
Blog

Blog

— this points to a file called '...","datePublished":"2021-06-11T15:53:23.712Z","isAccessibleForFree":true,"author":{"@type":"Person","name":"Codewell","url":"https://hashnode.com/@codewell","sameAs":"http://twitter.com/codewell_"},"publisher":{"@type":"Organization","name":"Blog","url":"https://blog.codewell.cc","logo":"https://hashnode.com/utility/r?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1559814205701%2Fek9fO-yT0.jpeg%3Fw%3D800%26bm%3Dnormal%26balph%3D100%26txt64%3DQmxvZw%26txtsize%3D42%26txtfit%3Dmax%26txtalign%3Dmiddle%2Ccenter%26txtfont%3DHelvetica%20Neue%2CBold%26txtclr%3D000000%26blend%3Dffffff"},"image":{"@type":"ImageObject","url":"https://cdn.hashnode.com/res/hashnode/image/upload/v1623426788507/0clEEeGse.png"}}
How To Link Javascript to HTML Using the Src Attribute

How To Link Javascript to HTML Using the Src Attribute

Codewell's photo
Codewell
·Jun 11, 2021·

1 min read

Subscribe to my newsletter and never miss my upcoming articles

To link a JavaScript file to an HTML document, you need to use the <script> tag.

The Src Attribute

The src attribute allows you to point to a URL that has an external script file.

<script src="js/script.js"></script> — this points to a file called 'script.js' inside the folder 'js'.

You can also point it to an external absolute URL:

<script src="https://example.com/js/script.js"></script>

Where to place your script tag

You can place the script tag in:

  1. In between the <head> tags.
  2. Before the closing </body> tag.
  3. Both.
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width">
  <title>Codewell - Free Frontend Coding Challenges</title>

<script src="/javascript/script.js"></script>
</head>


<body>
  <!-- Content --> 
  <script src="/javascript/script.js"></script>
</body>
</html>

There are cases where you would want to place it in the <head> tag, and cases where you would want to place it at the bottom of the <body> tag. This article goes more in detail about it.

If you're just learning Javascript, read our blog on websites you can use to practice your skills here.

 
Share this