How to Place JavaScript in an HTML Tag
JavaScript can also be placed within HTML tags, as shown in the following example.
<BODY onLoad=document.write(“Hello World!”)> </BODY>
In this example, the JavaScript onLoad=document.write(“Hello World!”) statement has been added to the HTML <BODY> tag. This particular JavaScript statement tells the browser to write the enclosed text when the browser first loads the HTML page.
Placing small JavaScript statements inside HTML tags provides an easy way to execute small pieces of JavaScript code. Of course, this option really is beneficial only when executing small JavaScript statements and is impractical for larger JavaScript statements or situations that required multiple lines of code. However, as you will learn tomorrow, you can also trigger the execution of JavaScripts embedded in an HTML page’s head section by embedding calling statements inside HTML tags.