31
JAVASCRIPT HOW TO PROGRAM -2 DR. JOHN P. ABRAHAM UTPA

JAVASCRIPT HOW TO PROGRAM -2

Embed Size (px)

DESCRIPTION

JAVASCRIPT HOW TO PROGRAM -2. DR. JOHN P. ABRAHAM UTPA. Script development process. Requirement analysis Goal – Audience Design Sketch the page – Site map – outline content - pseudocode Implementation Coding Support & maintenance. Script statements in head. User defined functions - PowerPoint PPT Presentation

Citation preview

Page 1: JAVASCRIPT HOW TO PROGRAM -2

JAVASCRIPTHOW TO PROGRAM -2

DR. JOHN P. ABRAHAMUTPA

Page 2: JAVASCRIPT HOW TO PROGRAM -2

Script development process Requirement analysis

Goal – Audience Design

Sketch the page – Site map – outline content - pseudocode

Implementation Coding

Support & maintenance

Page 3: JAVASCRIPT HOW TO PROGRAM -2

Script statements in head

User defined functions Global variables Statements that preload images

for use in rollover effects

Page 4: JAVASCRIPT HOW TO PROGRAM -2

Keep an HTML outline<HTML><head><title> Array example </title><script language ="javaScript">

</script></head><body></body></HTML>

Page 5: JAVASCRIPT HOW TO PROGRAM -2

InputBox<HTML><head><title> Ask user name </title><script language ="javaScript">

var visitor = prompt("What is your name?", " ")

</script></head><body></body></HTML>

Page 6: JAVASCRIPT HOW TO PROGRAM -2

Using input<HTML><head><title> Ask user name </title><script language ="javaScript">

var visitor = prompt("What is your name?", “Default Name ")

</script></head><body><script language ="javascript">document.write("<h1> Hello, ", visitor, "! </h1>")</script></body></HTML>

Page 7: JAVASCRIPT HOW TO PROGRAM -2

Onload (event)<HTML><head><title> Ask user name </title><script language ="javaScript">

var visitor = prompt("What is your name?", " ")

</script></head><body onLoad="alert('Welcome! '+visitor)">

</body></HTML>

Page 8: JAVASCRIPT HOW TO PROGRAM -2

Alert Box with 2 lines<HTML><head><title> Ask user name </title><script language ="javaScript">

var visitor = prompt("What is your name?", " ")

</script></head><body onLoad="alert('Welcome! \n'+visitor)">

</body></HTML>

Page 9: JAVASCRIPT HOW TO PROGRAM -2

Function with return value<html><head><script type="text/javascript">function product(a,b){return a*b;}</script></head>

<body><script type="text/javascript">document.write(product(4,3));</script>

<p>The script in the body section calls a function with two parameters (4 and 3).</p>

<p>The function will return the product of these two parameters.</p></body></html>

Page 10: JAVASCRIPT HOW TO PROGRAM -2

Looping<HTML><head><title> Nested Loop </title><script language ="javaScript">var i=1, j; while (i <= 12) {document.writeln("<br>Table for ",i, "<br>"); for (j=1; j <=16; j++) document.writeln(i, " x ", j, " = ", i*j,"<br>"); i++ }</script></head><body></body></HTML>

Page 11: JAVASCRIPT HOW TO PROGRAM -2

Table

 table is divided into rows (with the <tr> tag),

each row is divided into data cells (with the <td> tag). td stands for "table data," and holds the content of a data cell. A <td> tag can contain text, links, images, lists, forms, other tables, etc.

Page 12: JAVASCRIPT HOW TO PROGRAM -2

Looping & Table<HTML><head><title> Nested Loop </title><script language ="javaScript">

var i=1, j; while (i <= 12)

{document.writeln("<table border='1'>"); document.writeln("<br><tr><td>Table for ",i, "</td></tr>"); for (j=1; j <=16; j++) document.writeln("<tr><td>",i, " x ", j, " = ",

i*j,"</td></tr>"); document.writeln("</table>"); i++ }</script></head><body></body></HTML>

Page 13: JAVASCRIPT HOW TO PROGRAM -2

Assignment Help<HTML><HEAD><TITLE>Future value of an investment</TITLE><SCRIPT LANGUATE = "JavaScript"> var age, initial, deposit, rate; getdata(); if (rate >1) rate = rate/100; //convert rate to fraction just in case.

document.writeln( age," ", initial," ", deposit," ", rate);

function getdata() {

document.writeln("<BR> This program calculates your total assets at retirment (age 65)");document.writeln("<BR> given initial deposit, amount of deposit every 30 days, ");document.writeln("<BR> interest rate and your current age.");document.writeln("<BR>---------------------- data entry ---------------------------<BR>");age=parseInt(window.prompt("How old are you (must be below 65)?----------------> "));

initial=parseFloat(window.prompt( "What is your initial deposit?----------------------> "));deposit=parseFloat(window.prompt( "Amount you agree to deposit every 30 days?---------> "));rate=parseFloat(window.prompt( "What would be your expectation of interest or growth? "));return initial, deposit, rate, age;

}</Script></HEAD></HTML>

Page 14: JAVASCRIPT HOW TO PROGRAM -2

JavaScript Arrays

Dr. John P. AbrahamProfessor

UTPA

Page 15: JAVASCRIPT HOW TO PROGRAM -2

Declaring & populating an Array

var students = new array()students[0] = “Harry”students[2]= “Jim”var months= new

Array(“Jan”,”Feb”…”Dec”)

--display—For (i=0; I <= 12; i++)

{document.write(i, “. “,months[i], <br>’)

Page 16: JAVASCRIPT HOW TO PROGRAM -2

Stack Operationsvar stack = new Array();function createStack(top) {top = 0; }function destroyStack(top)

{top = 0;}

function emptyStack(top ) {

return (top = 0);}

function push(element) {top = top + 1; stack[top] = element; }function pop() {element = stack[top]; top = top - 1; return element; }

Page 17: JAVASCRIPT HOW TO PROGRAM -2

Parsing a StringprobLen = problem.length;

while (j <= probLen-1) { onechar = problem.charAt(j); while ((onechar !=" ") && (j <=probLen-1)) {

parse(); j++; onechar = problem.charAt(j);

}//while inner if (nums != "") pushStack() else if (opcode != "") domath(); j++;}//while outer

Page 18: JAVASCRIPT HOW TO PROGRAM -2

Function parsefunction parse(){

if (onechar >= "." && onechar <= "9") {if (onechar != "/") nums = nums +

onechar; else opcode = onechar;

} else opcode = onechar;

}

Page 19: JAVASCRIPT HOW TO PROGRAM -2

Forms and Form Processing (CGI)

Dr. John P. AbrahamUTPA

Page 20: JAVASCRIPT HOW TO PROGRAM -2

Purpose of forms

Collect information for the users (user input collection)

Sent from browsers (clients) to servers

Page 21: JAVASCRIPT HOW TO PROGRAM -2

Sample form<form method="post"

action="mailto:[email protected]">Name: <input type="text" size="10"

maxlength="40" name="name"> <br />Password: <input type="password" size="10"

maxlength="10" name="password"><br /><INPUT type="radio" name="sex"

value="Male"> Male<BR><INPUT type="radio" name="sex"

value="Female"> Female<BR><input type="submit" value="Send"> </form>

Page 22: JAVASCRIPT HOW TO PROGRAM -2

What is a form

Form element of HTML Designed to collect input Mail to vs. server-side scripts

mailto URL causes the form data to sent to an email address.

No server-side scripting is required. Server-side scripts. CGI, Perl, PHP, ASP,

ColdFusion. Can write the data to a database.

Page 23: JAVASCRIPT HOW TO PROGRAM -2

Post vs. Get

Post method – form data is sent separately from the actual call to the server-side script. (data is sent under separate cover) Preferred method

Get method: the data is appended to the end of the URL that calls a server-side script. Easy to see what is being sent.

Page 24: JAVASCRIPT HOW TO PROGRAM -2

Input types

Text Selections –

radio buttons Check boxes Pull-down menus (pick one for State) Hidden input elements (such as

session info)

Page 25: JAVASCRIPT HOW TO PROGRAM -2

Introduction to CGI Data sent to server require customized

processing on the server side. Common Gateway Interface governs the

way a web-server (Apache, IIS, etc.) interacts with an external program.

A popular scripting language conforms to CGI is PERL.

CGI programs can be written any language.

Page 26: JAVASCRIPT HOW TO PROGRAM -2

Browsers and CGI End user clicks the submit button

Form’s data are sent over the internet to the web server.

Web server upon receipt of the requests executes the correct application program Forwards the input data to that program

The application program performs requested steps. Generates output back to the server in HTML

Web server forwards the output to the client

Client browser displays it

Page 27: JAVASCRIPT HOW TO PROGRAM -2

Outline of a CGI program Determines request method (get or

post) and receives input data. Decodes and checks input data. Performs tasks Produces output in HTML format

Page 28: JAVASCRIPT HOW TO PROGRAM -2

Perl programming language

Practical Extension and Reporting Language

1987 Larry Wall at NASA Free Provides a CGI interface module Portable application (runs on web

servers on different platforms)

Page 29: JAVASCRIPT HOW TO PROGRAM -2

CGI-tutorial

http://www.cgi101.com/book/ch1/text.html

Page 30: JAVASCRIPT HOW TO PROGRAM -2

Example of a CGI program

#!/usr/bin/perl print "Content-type: text/html\n\n"; print "<html><head><title>Hello

World</title></head>\n"; print "<body>\n"; print "<h2>Hello,

world!</h2>\n"; print "</body></html>\n";

Page 31: JAVASCRIPT HOW TO PROGRAM -2

HTML5 It is time for you (and me) to learn

HTML5 I suggest you go to this site and

follow the tutorial.http://w3schools.com/html/html5_intro.asp