Very quick guide to JavaScript for Java programmers - CIS Personal ...

9 downloads 347 Views 231KB Size Report
2/8. Basics. • Often interacts with forms. – Checks forms before submission. – Automatically completes/calculates fields. • Dynamic typing => no type statements.
Very quick guide to JavaScript for Java programmers Mark Dunlop

Basics • Often interacts with forms – Checks forms before submission – Automatically completes/calculates fields • Dynamic typing => no type statements • Like Java . is used for separating parts of a variable e.g. object.field

2/8

Example field completion function init(form) { // puts the date (in non-US format into the date field of form date = new Date(); form.date.value=date.getDate()+"/"+ (date.getMonth()+1)+"/"+date.getFullYear(); } • to call add info to body tag of HTML page and to form tag ... 3/8

Functions • Functions replace methods in typical JavaScript these are non-OO • They do not specify a return type nor the type of parameters • Return clauses are optional

4/8

Function Example function addHello(myName){ if (myName=="") return ("Hello friend"); else return ("Hello"+myName); }

5/8

Simple user interaction • window.alert(String)

• Boolean window.confirm(String)

• String window.prompt(String)

6/8

Submission example function checkUserIsHappy(message){ return (window.confirm(message)); } • add to submit input tag Note quotes! Use careful wording – you get OK/Cancel with OK as default

7/8

Java Damage

8/8