JavaScript is a programming language to make a browser perform simple operations. JavaScript can tell your browser to do something every few seconds. JavaScript can tell your browser to react to the location of the mouse. JavaScript can look at cookie files and use that information to tailor the content of your web page. JavaScript can even look at your browsing history.
It is important to remember that JavaScript commands run on your machine inside your browser. The commands are not running on the web server machine. Running commands on your machine that your browser retrieved from a web site can be good, but can also be dangerous. You can set your browser to not run any JavaScript commands. But because JavaScript is used in nearly all web sites, turning off JavaScript means most sites will not work properly. Our purpose in covering JavaScript in CSCI 101 is for you to understand what your browser is capable of doing while you are surfing the web.
JavaScript commands are mixed in with HTML tags. Simple JavaScript commands go inside HTML tags. More complex commands to be run by the browser go inside the body between <script> and </script> tags. Commands can also be grouped together as a "function" and placed in the head section.
You can create your own functions inside the <Head> section. For example,
<SCRIPT>
function change_image() {document.changing_pic.src = "new_pic.jpg";}
</SCRIPT>
This JavaScript code creates a new function named "change_image" which will look inside the
<Body> and find an <img> named "changing_pic" and then reset the source of that
<img>
to "new_pic.jpg. In other words, that function makes a picture change.
An HTML tag inside the <Body> could trigger our function to change
the picture.
The JavaScript language has a syntax, just like English has a syntax. In JavaScript functions the brackets, { and }, mark the start and end of your function. The semicolons, ;, mark the end of each instruction. This example function contains just one instruction, but a function could have lots of instructions.