Exploring JavaScript Ch 14 JavaScript brings a dynamic functionality to your websites
JavaScript and HTML Text A typical HTML 4.01 “Hello World” document using JavaScript might look like Example14-1. Example 14-1. “Hello World” displayed using JavaScript <html> <head><title>Hello World</title></head> <body> <script type="text/javascript”> document.write("Hello World”) </script> <noscript> Your browser doesn't support or has disabled JavaScript </noscript> </body> </html>
Using Scripts Within a Document Head you can put it in the <head> section Old and non Standard Browsers Other scripting languages Microsoft’s VBScript, which is based on the Visual Basic programming language and Tcl, a rapid prototyping language
Including JavaScript Files In addition to writing JavaScript code directly in HTML documents, you can include files of JavaScript code either from your website or from anywhere on the Internet. The syntax for this is <script type="text/javascript" src="script.js"></script> To pull a file in from the Internet, use <script type="text/javascript" src=http://someserver.com/script.js></script>
Debugging JavaScript Errors
Using Comments // This is a comment /* This is a section of multiline comments that will not be interpreted */
Semicolons Not generally required by JavaScript if you have only one statement on a line
Variables, String Variables No particular character identifies a variable in JavaScript as the dollar sign does in PHP JavaScript string variables should be enclosed in either single or double quotation marks, like this: greeting = "Hello there” warning = 'Be careful’
Operators, Arithmetic Operators JavaScript mathematical operators look a lot like plain arithmetic
Assignment Operators
Comparison Operators
Logical Operators do not include and and or equivalents to && and ||, and there is no xor operator
Variable Typing
Functions, Global Variables As with PHP, JavaScript functions are used to separate out sections of code that perform a particular task see 14-9 Global variables are ones defined outside of any functions (or within functions, but defined without the var keyword).
Local Variables Parameters passed to a function automatically have local scope see example 14-10, 14-11 and 14-12
The Document Object Model Document Object Model or DOM. This breaks down the parts of an HTML document into discrete objects, each with its own properties and methods, and each subject to JavaScript’s control.
DOM Object Hierarchy