Embed Javascript code in HTML

Javascript can be included in HTML by whether embedding it to the HTML file itself, or by calling an external file.

There are few options to include Javascript code into HTML, but current best practice is as in the example below;

<script type="text/javascript">
    window.alert("Hello World")
</script>

The above example using type and MIME type, when called from anywhere in the HTML page will pop up an alert with the Hello World text.

Sign up for The Complete JavaScript Course: Build Real Projects! and many other great courses on Udemy to learn more.


Written by Mohd Shakir Zakaria. Last updated on 2019-02-12