putting javascript in html code example

Example 1: include script in html

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

Example 2: how to use javascript in html

<!-- include an external javascript document -->
<script src="myJavaScript.js" charset="utf-8"></script>

<!-- JavaScript inside html element -->
<script>
  console.log("This is JavaScript");
</script>

Example 3: how to put javascript in HTML

<script>
  //put your java script here
  </script>

Example 4: how to add script in html head

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

<!--just add defer attribute so script will execute at last-->

Tags:

Html Example