HTML: Embed JavaScript

By Xah Lee. Date: .

Embed javascript in html

Embed JavaScript file

<script defer src="xtest.js"></script>

you can place this anywhere inside the HTML head tag, or anywhere inside in body tag.

  1. Create a file named xtest.html, with the above content.
  2. Create a file named xtest.js, with the content console.log("good morn");.
  3. Open the file xtest.html in browser.

to see the output, use browser console.

Embed JavaScript code

in HTML file, include the following

<script>
console.log("good morn");
</script>

You should put this near the end of the HTML file, right above the closing body tag.

Embedding code like this is not recommended, unless the code is just few lines.

Loading JavaScript in browser