Hello | HTML run JavaScript
ThispartofthetutorialisaboutcoreJavaScript,thelanguageitself.Butweneedaworkingenvironmenttorunourscriptsand,sincethisbookisonline,thebrowserisagoodchoice.We’llkeeptheamountofbrowser-specificcommands(likealert)toaminimumsothatyoudon’tspendtimeonthemifyouplantoconcentrateonanotherenvironment(likeNode.js).We’llfocusonJavaScriptinthebrowserinthenextpart[1]ofthetutorial.Sofirst,let’sseehowweattachascripttoawebpage.Forserver-sideenvironments(likeNode.js),youcanexecutethescriptwithacommandlike"node...
This part of the tutorial is about core JavaScript, the language itself.
But we need a working environment to run our scripts and, since this book is online, the browser is a good choice. We’ll keep the amount of browser-specific commands (like alert) to a minimum so that you don’t spend time on them if you plan to concentrate on another environment (like Node.js). We’ll focus on JavaScript in the browser in the next part[1] of the tutorial.
So first, let’s see how we attach a script to a webpage. For server-side environments (like Node.js), you can execute the script with a command like "node my.js".
JavaScript programs can be inserted almost anywhere into an HTML document using the <script> tag.
For instance:
<!DOCTYPE HTML> <html> <body> <p>Before the script...</p> <script> alert( Hello, world! ); </script> <p>...After the script.</...