1 javascript

42
1 JavaScript http://jjcweb.jjay.cuny.edu/ssengupta/

Upload: gervase-bradley

Post on 05-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 JavaScript

1

JavaScript

http://jjcweb.jjay.cuny.edu/ssengupta/

Page 2: 1 JavaScript

JavaScript Intro

“JavaScript is the most popular scripting language on the internet, and works in all major browsers, such as Internet Explorer, Firefox, Chrome, Opera, and Safari”

What is JavaScript?o JavaScript is a scripting languageo Designed to add interactivity to HTML pageso A lightweight programming languageo Can be embedded directly into HTML pageso Everyone can use JavaScript without purchasing a license

2

Page 3: 1 JavaScript

Java and JavaScript

Are Java and JavaScript the same?o NO!

Java and JavaScript are two completely different languages in both concept and design!

Java (developed by Sun Microsystems) is a programming language - in the same category as C and C++.

3

Page 4: 1 JavaScript

What can a JavaScript do?

JavaScript gives HTML designers a programming tool JavaScript can put dynamic text into an HTML page

o A JavaScript statement like this: document.write("<h1>" + name + "</h1>") can write a variable text into an HTML page

JavaScript can react to events o A JavaScript can be set to execute when something happens, like when a

page has finished loading or when a user opens an HTML element

JavaScript can be used to validate data o 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 o 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 create dynamic effects in an HTML page

4

Page 5: 1 JavaScript

JavaScript – How to start

The HTML <script> tag is used to insert a JavaScript into an HTML page.

<html><body><script type="text/javascript">document.write("Hello World!");</script></body></html>

5

Page 6: 1 JavaScript

JavaScript – How to (contd.)

You can even add HTML tags to the JavaScript

<html><body><script type="text/javascript">document.write("<h1>Hello World!</h1>");</script></body></html>

6

Page 7: 1 JavaScript

How to Handle Simple Browsers

Browsers that do not support JavaScript, will display JavaScript as page content.

To prevent them from doing this, and as a part of the JavaScript standard, the HTML comment tag should be used to "hide" the JavaScript.

<html><body><script type="text/javascript"><!--document.write("Hello World!");//--></script></body></html>

7

Page 8: 1 JavaScript

Where to put the JavaScript

Scripts can be in <body>

<html><head></head>

<body><script type="text/javascript">document.write("This message is written by JavaScript");</script></body>

</html>

8

Page 9: 1 JavaScript

Where to put the JavaScript

Scripts can be in <head>

<html><head><script type="text/javascript">function message(){alert("This alert box was called with the onload event");}</script></head>

<body onload="message()"></body></html>

9

Page 10: 1 JavaScript

Using an External JavaScript

If you want to run the same JavaScript on several pages, without having to write the same script on every page, you can write a JavaScript in an external file

Save the external JavaScript file with a .js file extension

Note: The external script CANNOT contain the <script></script> tags!

To use the external script, point to the .js file in the "src" attribute of the <script> tag:

10

Page 11: 1 JavaScript

Using an External JavaScript

<html><head>

<script type="text/javascript" src=“abc.js"></script></head><body></body></html>

11

Page 12: 1 JavaScript

JavaScript Code

JavaScript code (or just JavaScript) is a sequence of JavaScript statements.

A JavaScript statement is a command to a browser.o The purpose of the command is to tell the browser what to

do.o document.write("Hello World");o alert("This alert box was called with the onload event");

JavaScript is Case Sensitiveo Unlike HTML, JavaScript is case sensitive - therefore watch

your capitalization closely when you write JavaScript statements, create or call variables, objects and functions

12

Page 13: 1 JavaScript

JavaScript Comments

Comments can be added to explain the JavaScript, or to make the code more readable

Single line comments start with //

JavaScript Multi-Line Commentso Multi line comments start with /* and end with */

13

Page 14: 1 JavaScript

Let’s create some simple JavaScript Code

14

Page 15: 1 JavaScript

JavaScript Operators

JavaScript can be used to do Math or Math-like operationso Adding strings or numbers

JavaScript Operatorso = is used to assign valueso + is used to add valueso var is used to declare variables inside JavaScript

15

Page 16: 1 JavaScript

JavaScript Code

Adding two strings together and display:

<html><head></head><body><script type="text/javascript">var txt1="What a very";var txt2="nice day";var txt3;txt3=txt1+txt2;document.write(txt3);</script></body></html>

16

Page 17: 1 JavaScript

JavaScript Code (contd.)

<html><head></head><body><script type="text/javascript">var txt1="What a very";var txt2=" nice day";var txt3;var txt4=" Hello world";txt3=txt1+txt2;txt5=txt3+txt4;document.write(txt5);</script></body></html>

17

Page 18: 1 JavaScript

JavaScript Arithmetic Operators

18

Page 19: 1 JavaScript

JavaScript Comparison Operators

Comparison operators can be used in conditional statements to compare values and take action depending on the result:

if (age<18) document.write("young");

19

Page 20: 1 JavaScript

JavaScript Comparison Operators

In JavaScript we have the following conditional statements:

o if statement - use this statement to execute some code only if a specified condition is true

o if...else statement - use this statement to execute some code if the condition is true and another code if the condition is false

o if...else if....else statement - use this statement to select one of many blocks of code to be executed

o switch statement - use this statement to select one of many blocks of code to be executed

20

Page 21: 1 JavaScript

If Statement

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

21

Page 22: 1 JavaScript

If...else Statement

if (condition)  {  code to be executed if condition is true  }else  {  code to be executed if condition is not true  }

22

Page 23: 1 JavaScript

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  }

23

Page 24: 1 JavaScript

JavaScript Switch Statement

switch(n){case 1:  execute code block 1  break;case 2:  execute code block 2  break;default:  code to be executed if n is different from case 1 and 2}

24

Page 25: 1 JavaScript

JavaScript Objects Introduction

JavaScript is an object oriented programo Provides dynamic and customized effect in

web design with these objects

Date Objecto Used to work with dates and times

25

Page 26: 1 JavaScript

JavaScript Date Object The Date object is used to work with dates and times  Date objects are created with the Date() constructor

new Date() // current date and time

var d=new Date();document.write(d);document.write(d.getDay());

26

<script type="text/javascript">var d=new Date();document.write(d.getDay());</script>

Page 27: 1 JavaScript

Date Object Methods

27

Page 28: 1 JavaScript

Practice Exercise

Create a webpage using HTML and JavaScript that dynamically displays greetings depending on hour of the day.

Anytime before 10 am: good morning After 10 am and before 3 pm: good day After 3 pm and before 8 pm: good evening After 8 pm and before 11 pm: good night

Hint:o Use Date object and if…else if… statements

28

Page 29: 1 JavaScript

Part 2

29

Page 30: 1 JavaScript

JavaScript String Object

The String object is used to manipulate a stored piece of text

30

var txt="Hello world!";document.write(txt.length);

var txt="Hello world!";document.write(txt.toUpperCase());

var txt="Hello world!";

var txt=5;

Page 31: 1 JavaScript

String manipulation

JavaScript can be used to manipulate and display texts in various formatso Big size texto Small size texto Boldo Italicso Font coloro Subscript / Superscripto Blink

31

Page 32: 1 JavaScript

Example: String manipulation

32

Page 33: 1 JavaScript

Hyperlink in JavaScript

33

Page 34: 1 JavaScript

Practice Exercise

Create a JavaScript code with blinking hyperlink

34

Page 35: 1 JavaScript

JavaScript Popup Boxes JavaScript has three kinds of popup boxes:

o Alert boxo Confirm box and o Prompt box

Alert box is often used to provide certain information to the usero When an alert box pops up, the user clicks "OK" to proceed

Confirm box is often used if you want the user to verify or accept somethingo When a confirm box pops up, the user will have to click either

"OK" or "Cancel" to proceed Prompt box is often used if you want the user to input a

value before entering a page

35

Page 36: 1 JavaScript

Alert Box

<html><head><script type="text/javascript">function show_alert(){alert("Hello! I am an alert box!");}</script></head><body><form><input type="button" onclick="show_alert()" value="Show alert box" /></form></body></html>

36

Page 37: 1 JavaScript

Confirm Box<html><head><script type="text/javascript">function show_confirm(){var r=confirm("Press a button!");if (r==true) { alert("You pressed OK!"); }else { alert("You pressed Cancel!"); }}</script></head><body><form><input type="button" onclick="show_confirm()" value="Show confirm box" /></form></body></html>

37

Page 38: 1 JavaScript

Practice Exercise

Create a confirm box button just like the previous example.

When the user presses OK or Cancel, display “You pressed OK!” or “You pressed Cancel!” as blinking text in the html webpage.

38

Page 39: 1 JavaScript

Prompt Box<html><head><script type="text/javascript">function show_prompt(){var name=prompt("Please enter your name","Harry Potter");if (name!=null && name!="") { document.write("Hello " + name + "! How are you today?"); }}</script></head><body><form><input type="button" onclick="show_prompt()" value="Show prompt box" /></form></body></html>

39

Page 40: 1 JavaScript

Extra Credit Solution Discussion

40

Page 41: 1 JavaScript

Announcements: Imp dates

Dec. 2nd : HTML quiz (Regular class timing)

Dec 7th : Final exam preview and quiz answer discussion; Project Submission

Dec. 9th : No in-class meeting

Dec. 16th : Final Exam

41

Page 42: 1 JavaScript

HTML Quiz preview

42