Dynamic Web Pages
JavaScript is a just-in-time (or runtime) compiled language that can be used to add interactivity and dynamic elements to your site. Although people typically think of the browser when JavaScript comes to mind, it can be used in many different environments (like Node.js on the back-end).
Embed or Include
Much like CSS, JavaScript can be included within your HTML file, or can be contained within an external file. A < script > element is used to add JavaScript directly to an HTML file.
Output
JavaScript can display output in multiple places, so choose what’s right for your needs.
- If you’d like to pester your user with those annoying pop-up messages, then the alert() function may be the choice for you.
- If you’d like to have an error or status message displayed somewhere the average user isn’t going to see, then use console.log() to send a message to the console.
- If you’d like to print directly to the rendered page, you can use the document.write() function to change content on the page.
At some point, you’re going to want to add interactivity to your site by receiving input from the user. Two methods that utilize pop-ups are the propmpt() and confirm() functions.
- If you need to receive new information from the user, you can use prompt() to return the content of their answer for further use.
- If you just need the user to make a pre-defined choice, confirm() is the way to go. You script a yes/no situation, and the outcomes for either answer, and the user is provided with the outcome that matches their answer.