Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to JavaScript MIS 3502, Fall 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 9/29/2016.

Similar presentations


Presentation on theme: "Introduction to JavaScript MIS 3502, Fall 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 9/29/2016."— Presentation transcript:

1 Introduction to JavaScript MIS 3502, Fall 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 9/29/2016

2 Objectives Slide 2 Where does JavaScript run? How is JavaScript syntax different from PHP? Where does the JavaScript go? What’s a good basic use of JavaScript?

3 Where does JavaScript run? Slide 3 URL, referencing a.php page HTTP Response Database PHP Interpreter Browser JavaScript Engine As a general rule, JavaScript lives here, in the browser, and does not interact with any resource outside the browser. This is called a “round trip”

4 Discuss: Similarities and Differences Slide 4 How is JavaScript syntax similar to PHP? Both draw syntax conventions from the C programming language Statements end in semicolon (usually) Use curly braces {} for code blocks functions declared and used in a similar manner Comparison and assignment operators the same Conditional statements and loops work the same. Case sensitive

5 Discuss: Similarities and Differences Slide 5 How is JavaScript syntax different from PHP? Variables do not need to start with $ JavaScript code is visible via the “View Source” feature of your browser. It appears inside the tag. Use document.write("Hello World!"); instead of echo("Hello World!"); Concatenation character is “ + ” not “. ” There is no variable substitution in strings. The library of functions is different Variables are declared using var The object operator is different. JavaScript uses a “.” and not a “ -> ” JavaScript has what’s known as the Document Object Model

6 The DOM (Document Object Model) Slide 6 JavaScript can change all the HTML elements in the page JavaScript can change all the HTML attributes in the page JavaScript can change all the CSS styles in the page JavaScript can remove existing HTML elements and attributes JavaScript can add new HTML elements and attributes JavaScript can react to all existing HTML events in the page JavaScript can create new HTML events in the page Source: http://www.w3schools.com/js/js_htmldom.asp All of these activities are accomplished through the properties and methods of objects.

7 Slide 7 We will see an example of this momentarily…

8 Where does JavaScript go? Slide 8 You can put a tag here, in the head. (My personal preference.) Or here… right before the closing body tag.

9 Where does JavaScript go? (2) Slide 9 Another option is to use the src attribute of the tag.

10 Let’s give it a try… Slide 10 See the example javascriptdemo.html Review its functionality. Try it out.

11 Lot’s of things, actually. Whole applications can be written in JavaScript. Soon we will see how it can be used to asynchronously pull data from a web service. But for today we’ll consider a more simple case: form validation. In the most general terms, we use JavaScript to cut down on … So… what’s JavaScript good for? Slide 11

12 JavaScript for form validation Slide 12 Users can be notified of problems before their form is ever posted to the server Discuss: does this eliminate the need for server side validation of user provided data? Why?

13 To do Slide 13 See the example javascriptdemo2.html Review its functionality. Make special note of: –The onsubmit attribute of the form. If onsubmit is set to false, the form will not submit. –The user defined function named "validate". It will return either true or false. Complete the validation function so that all form fields are validated. –Note that JavaScript gives you a function called isNaN (short for “is not a number”) that returns true or false.

14 It’s time for a challenge Slide 14


Download ppt "Introduction to JavaScript MIS 3502, Fall 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 9/29/2016."

Similar presentations


Ads by Google