|
How do you use JavaScript?JavaScript code is placed inside HTML files, which means you can create JavaScript code the same way you create HTML, by using any basic text editor such as MS Word, WordPad, NotePad or BBEdit. Most JavaScript code is placed inside <SCRIPT> tags, like this:
Usually, these tags are placed in the head of the document, between the <HEAD> and </HEAD> tags. This is the section of the document that loads first, and putting your JavaScript code there guarantees that it will be loaded and recognized by the browser before the user interacts with it. JavaScript is driven by Events, which are things that happen on a page, usually as a result of user actions. JavaScript uses Event Handlers to respond to Events. Event Handlers are written into various types of HTML tags and don't require a <SCRIPT> tag. For example, the following combination of HTML and JavaScript will give the user a message. Here's what the code looks like:
The term "onClick" is the Event Handler, and as you can see, it is included as an attribute inside the HTML <INPUT> tag. When a user clicks on the button, the JavaScript code (within the quotation marks) is activated. NOTE: the JavaScript code is within the quotation marks "alert('Thank you.')" or "JavaScript code something1; JavaScript code something2; ...; JavaScript code somethingN" The command "alert()" causes a dialog box to appear with the words 'Thank you' inside. And here's how it works: We will go into all of this in greater detail throughout the course. For now, it's only important that you understand how JavaScript code is included in HTML documents |