This tutorial is part of a web programming course for people who have no programming experience.
Put the Code Inside a SCRIPT Element
<script> var message = "Hello World!"; alert(message); </script>
Normally the SCRIPT element goes inside the HEAD element, but it doesn't necessarily have to go there. In a future lesson we'll see how the placement of the SCRIPT element can affect your program.
We'll use this approach for most of the code samples that we write in this course because it's convenient for use to keep our code in the same web page that demos it.
alert("Hello from test.js!");
Then we created a sample web page and put this code inside the HEAD element:
In summary, here are the benefits to putting code in separate .js files and linking to them from your web page:
- It makes it easy to update/maintain your site, because you can make a change to one .js file and then all the pages linking to it will automatically be updated.
Use Various Attributes
<h1 x="alert('Hello from H1');">Embedding JS Code in a Page<h1>