1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight.

Similar presentations

Presentation on theme: "1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight."— Presentation transcript:

1
1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight programming language A JavaScript consists of lines of executable computer code A JavaScript is usually embedded directly into HTML pages JavaScript is an interpreted language (means that scripts execute without preliminary compilation) Everyone can use JavaScript without purchasing a license

2
2 What can a JavaScript Do? JavaScript gives HTML designers a programming tool - HTML authors are normally not programmers, but JavaScript is a scripting language with a very simple syntax! Almost anyone can put small "snippets" of code into their HTML pages JavaScript can put dynamic text into an HTML page - A JavaScript statement like this: document.write(" " + name + " ") can write a variable text into an HTML page JavaScript can react to events - A JavaScript can be set to execute when something happens, like when a page has finished loading or when a user clicks on an HTML element JavaScript can read and write HTML elements - A JavaScript can read and change the content of an HTML element

3
3 What can a JavaScript Do? JavaScript can be used to validate data - A JavaScript can be used to validate form data before it is submitted to a server. This saves the server from extra processing JavaScript can be used to detect the visitor's browser - A JavaScript can be used to detect the visitor's browser, and - depending on the browser - load another page specifically designed for that browser JavaScript can be used to create cookies - A JavaScript can be used to store and retrieve information on the visitor's computer

4
4 How to Put a JavaScript Into an HTML Page document.write("Hello World!")

5
5...

6
6 ">
">
" title="6 ">

7
7 Where to Put the JavaScript Head section Body section External script

12
12 Writing output to a page document.write is a standard JavaScript command for writing output to a page. document.write(“Hello, World!”) document is the object write is the method

13
13 JavaScript Popup Boxes Alert Box Confirm Box Prompt Box

14
14 Alert box An alert box is often used if you want to make sure information comes through to the user. When an alert box pops up, the user will have to click "OK" to proceed. Syntax: alert("sometext")

15
15 Confirm Box A confirm box is often used if you want the user to verify or accept something. When a confirm box pops up, the user will have to click either "OK" or "Cancel" to proceed. If the user clicks "OK", the box returns true. If the user clicks "Cancel", the box returns false. Syntax: confirm("sometext")

16
16 Prompt Box A prompt box is often used if you want the user to input a value before entering a page. When a prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed after entering an input value. If the user clicks "OK" the box returns the input value. If the user clicks "Cancel" the box returns null. Syntax prompt("sometext","defaultvalue")

17
17 JavaScript Variables Variables are used to store data. A variable's value can change during the script. You can refer to a variable by name to see its value or to change its value. Rules for variable names: Variable names are case sensitive They must begin with a letter or the underscore character

18
18 Declaring and Using a Variable It is declared by assigning it a value or using the var command var myVariable; var myVariable=“Hello”; myVariable=“Hello”; Variables should be declared immediately before using the variable

19
19 Using the Prompt to assign Variables The prompt allows the user to input a response The response can be stored in a variable to be used elsewhere var visitorName; visitorName=prompt(“What is your name?”, “Enter your name here.”);

22
22 JavaScript Conditional Statements Conditional statements in JavaScript are used to perform different actions based on different conditions.

23
23 If Statement if (condition) { code to be executed if condition is true }

24
24 If...else Statement if (condition) { code to be executed if condition is true } else { code to be executed if condition is not true }

25
25 If...else if...else Statement if (condition1) { code to be executed if condition1 is true } else if (condition2) { code to be executed if condition2 is true } else { code to be executed if condition1 and condition2 are not true }

27
27 JavaScript Functions A function is a reusable code-block that will be executed by an event, or when the function is called. Functions can be defined both in the and in the section of a document. However, to assure that the function is read/loaded by the browser before it is called, it could be wise to put it in the section.

28
28 How to Define a Function function functionname(var1,var2,...,varX) { some code }

40
40 JavaScript Events Events are actions that can be detected by JavaScript. Examples of events: A mouse click A web page or an image loading Mousing over a hot spot on the web page Selecting an input box in an HTML form Submitting an HTML form A keystroke