JavaScript - The Basics
Basics
- OO language
- Loosely typed
- Must declare variables with var
- Declare functions with function
- End lines with a semi-colon
- Include comments within /* ... */ or single-line
comments can start with //
- Close tie-in with HTML: e.g.
document.write()
will write to the current page.
- Can also manipulate the DOM
HTML Notes
- Often start with an empty or hidden <div> and
do something with it
- Often use the id= attribute to label and identify a
specific element
- HTML5 canvas allows drawings and graphics
Integrating JavaScript
Can have JavaScript inline:
<script type='text/JavaScript'>
function doSomething(in1, in2)
{
return(in1 * in2);
}
document.write(doSomething(5, 7));
</script>
Result
35
Can trigger JavaScript from buttons, links, etc.:
<button onclick='doSomething();'>Do Something!</button>
Can import JavaScript from a file:
<script src="myfile.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
Making things easier