250 likes | 271 Views
Introducing JavaScript. Goals. By the end of this lecture you should … Understand the purpose of JavaScript. Understand the basic rules governing JavaScript syntax. Understand how we can link our JavaScript applications to web pages. Goals (continued).
E N D
Goals By the end of this lecture you should … • Understand the purpose of JavaScript. • Understand the basic rules governing JavaScript syntax. • Understand how we can link our JavaScript applications to web pages.
Goals (continued) • Understand how events are central to JavaScript programming. • Understand how we can use built-in event handlers to detect events and program to those events.
What is JavaScript? • JavaScript is a client-side language. • JavaScript code executes within the confines of a web page. • JavaScript syntax is similar to C and Java (but, JavaScript is NOT Java!). • JavaScript deals with objects.
Review: What is an Object? • An object is a unique programming entity that has attributes to describe it (like adjectives in grammar) and methods to retrieve/set attribute values (like verbs in grammar).
Common JavaScript Objects • A web browser’s window • A string of characters • An integer number • Text in a web page • A menu in a web page • An image • A path to an image saved in memory
JavaScript Syntax • Spaces do not matter to the JavaScript interpreter. • Executable lines of JavaScript code end with a semi-colon:window.alert("Hi There!");
JavaScript Syntax • Programming structures begin and end with a curly brace:if(strUserName == "JOE"){//Executable lines //go here}//End If
A Note on Spacing & Indention • Use lots of white space to clarify your programming structures. • Use indention to differentiate between a structure's header and its body (executable block).
JavaScript Comments • Single line comments begin with 2 forward slashes://This is a comment.//These lines are also//single line//comments.
JavaScript Comments • You can also create comments to span multiple lines by beginning with /* and ending with */: /* This is a comment * that spans multiple * lines. */
Linking JavaScript to HTML • We can develop a JavaScript application directly inside a web page using a <script> … </script> container. • We can also develop our application in an external document and then link it by using the src attribute.
More on the <script> Tag • Normally, we nest <script> inside the <head>…</head> container. • The <script> tag has several attributes that give the browser important information about your application. Two important attributes are type and language.
The type Attribute • The type attribute defines the MIME type for your program. We'll always use the MIME value of text/javascript: <script type="text/javascript">
The language Attribute • The language attribute defines the version of JavaScript in which you code. We'll always use the MIME value of JavaScript 2.1: <script type="text/javascript" language="JavaScript 2.1">
Take the next few minutes to examine the file called introToJavaScript_01.html.
The src Attribute • We can link external JavaScript applications using the src attribute: <script type="text/javascript" language="JavaScript 2.1" src="scripts/hiThere.js">
Take the next few minutes to examine the file called introToJavaScript_02.html.
Detecting a Web Page Event • We write our scripts so they execute by reacting to events within a web browser. • Since events are at the heart of JavaScript programming, we need a way to detect events. We do that by using built-in event handlers …
onAbort onBlur onChange onClick onError onFocus onLoad onMouseOut onMouseOver onSelect onSubmit onUnload JavaScript Event Handlers Reference: JavaScript Event Handlershttp://www.intranetjournal.com/corner/hoque/eh-1.shtml
Take the next few minutes to examine the file called introToJavaScript_03.html.
The main Module (Function) • Most programs have a central point through which all operations process. Traditionally, programmers name this module (function) main. • We can tell main to execute when a page loads by using the onLoad event handler …
The onLoad Event Handler • The onLoad event handler detects a special event – that of a web page loading in a web browser. • We use onLoad to connect our JavaScript applications as an attribute of the <body> tag.
Take the next few minutes to re-visit the file introToJavaScript_01.html.See if you can find the event handler …
Summary • JavaScript is a client-side language that executes within in a web page. • One of the central ideas in JavaScript is writing code that deals with objects. • We detect events via event handlers, writing code to react to those events.