HTML: Embed JavaScript
Embed javascript in html
- The normal way for JavaScript to run, is to embed JavaScript code in a HTML file and view the file in browser.
- This is how web applications are written.
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.
- Create a file named
xtest.html, with the above content. - Create a file named
xtest.js, with the contentconsole.log("good morn");. - Open the file
xtest.htmlin 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.