The script in the HTML must be between the <script> and </script> tags. The script can be placed in the <body> and
1. Older instances may use the type= "Text/javascript" in the <script> tag. It is not necessary to do so now. JavaScript is the default scripting language in all modern browsers and HTML5.
2. Writing habits: The usual practice is to put functions into the
<!DOCTYPE HTML><HTML><Head><Script>functionmyFunction () {document.getElementById ("Demo"). InnerHTML="My First JavaScript Function";}</Script></Head><Body><H1>My Web Page</H1><PID= "Demo">A Paragraph.</P><Buttontype= "button"onclick= "myFunction ()">Click here</Button></Body></HTML>
3. External JavaScriptYou can also save the script to an external file. External files typically contain code that is used by multiple Web pages.
The file name extension for external JavaScript files is. js.
To use an external file, set the. js file in the "src" attribute of the <script> tag:
<!DOCTYPE HTML><HTML><Body><H1>My Web Page</H1><PID= "Demo">A Paragraph.</P><Buttontype= "button"onclick= "myFunction ()">Click here</Button><P><b>Comments:</b>MyFunction is saved in an external file named "Myscript.js".</P><Scripttype= "Text/javascript"src= "/js/myscript.js"></Script></Body></HTML>
Tip: External scripts cannot contain <script> tags.
JavaScript Learning the first day (how to use JavaScript)