Intro to JavaScript. JavaScript History Client (generally browser-side) language invented at Netscape under the name LiveScript around 1995 Netscape wanted.

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

Intro to Javascript CS Client Side Scripting CS380 2.
JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
Java Script Session1 INTRODUCTION.
Web Application Development Muhammad Ali Versonic Pte Asher Imtiaz Forman Christian College.
1 CSC 551: Web Programming Spring 2004 client-side programming with JavaScript  scripts vs. programs  JavaScript vs. JScript vs. VBScript  common tasks.
The Web Warrior Guide to Web Design Technologies
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Introduction to Scripting.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Javascript Client-side scripting. Up to now  We've seen a little about how to control  content with HTML  presentation with CSS  Javascript is a language.
Javascript II Expressions and Data Types. 2 JavaScript Review programs executed by the web browser programs embedded in a web page using the script element.
Introduction to JavaScript for Python Programmers
4.1 JavaScript Introduction
Scripting Languages.
Introduction to JavaScript 11 Introduction to Programming the WWW I CMSC Winter 2004 Lecture 14.
SYST Web Technologies SYST Web Technologies Lesson 6 – Intro to JavaScript.
WEEK 3 AND 4 USING CLIENT-SIDE SCRIPTS TO ENHANCE WEB APPLICATIONS.
Database-Driven Web Sites, Second Edition1 Chapter 3 INTRODUCTION TO CLIENT-SIDE SCRIPTS.
JavaScript & DOM Client-side scripting. JavaScript JavaScript is a tool to automate client side (which is implemented using HTML so far) JavaSript syntax.
Chapter 3 : Processing on the Front End JavaScript Technically its name is ECMA-262, which refers to the international standard which defines it. The standard.
1 JavaScript. 2 What’s wrong with JavaScript? A very powerful language, yet –Often hated –Browser inconsistencies –Misunderstood –Developers find it painful.
Week 9 PHP Cookies and Session Introduction to JavaScript.
Javascript. Outline Introduction Fundamental of JavaScript Javascript events management DOM and Dynamic HTML (DHTML)
SE-2840 Dr. Mark L. Hornick 1 Modifying webpage behavior using client-side scripting Javascript.
Introduction to JavaScript Gordon Tian
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
CHAPTER 4 Java Script อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
Using Client-Side Scripts to Enhance Web Applications 1.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
Introducing JavaScript. Goals By the end of this lecture you should … Be able to describe the differences among object methods, object properties and.
Javascript. What is JavaScript? Scripting (interpreted) language designed for the web Beware: JavaScript is case sensitive.
Dr. Qusai Abuein1 Internet & WWW How to program Chap.(6) JavaScript:Introduction to Scripting.
JavaScript Syntax, how to use it in a HTML document
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
05 – Java Script (1) Informatics Department Parahyangan Catholic University.
JavaScript, Fourth Edition
JavaScript. JavaScript is the programming language of HTML and the Web. Easy to learn One of the 3 languages of all developers MUST learn: 1. HTML to.
Introduction To JavaScript. Putting it Together (page 11) All javascript must go in-between the script tags. All javascript must go in-between the script.
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Java Script About Java Script Document Object Model Incorporating JavaScript Adding JavaScript to HTML Embedding a Javascript External Scripts Javascript.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
Chapter 5: Intro to Scripting CIS 275—Web Application Development for Business I.
Introduction to JavaScript MIS 3502, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 2/2/2016.
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
CGS 3066: Web Programming and Design Spring 2016 Introduction to JavaScript.
JavaScript and Ajax Week 10 Web site:
JavaScript Tutorial. What is JavaScript JavaScript is the programming language of HTML and the Web Programming makes computers do what you want them to.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Basic Objects. Math Object  Math.cos( x ), x in radians  Math.sqrt ( x )  Math.pow ( x, y )  Math.ceil( x )  etc.
 2001 Prentice Hall, Inc. All rights reserved. Outline 1 JavaScript.
CGS 3066: Web Programming and Design Spring 2017
>> Introduction to JavaScript
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
JavaScript Fundamentals
JavaScript Syntax and Semantics
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
JavaScript and Ajax (Expression and Operators)
JavaScript an introduction.
WEB PROGRAMMING JavaScript.
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
CS5220 Advanced Topics in Web Programming JavaScript Basics
An Introduction to JavaScript
CS3220 Web and Internet Programming JavaScript Basics
JavaScript CS 4640 Programming Languages for Web Applications
Introducing JavaScript
CS3220 Web and Internet Programming JavaScript Basics
Web Programming and Design
JavaScript CS 4640 Programming Languages for Web Applications
Presentation transcript:

Intro to JavaScript

JavaScript History Client (generally browser-side) language invented at Netscape under the name LiveScript around 1995 Netscape wanted a lightweight interpreted language to complement Java, similar to VB Script for Microsoft Name changed to JavaScript with introduction of Java to the browser; lots of confusion since between the two Standardized version is ECMAScript Now owned by Oracle

Adding JavaScript to a web page Inline code – place between tag function inlineScript() { var message = "Hello"; alert(message); } Generally placed in the header or at end of the tag if you want HTML to be rendered first before processing the JavaScript

External JavaScript file Use the src attribute to reference an external JavaScript file Inside demo.js: function printMessage() { var message = "Hello"; alert(message); }

Variables Loosely typed; case-sensitive - must be careful with case or programs will not work Declaring a variable var varName;// Semicolon optional but recommended Primitive Data Types Number (float and integer) String (Can use “” or ‘’) Boolean Undefined Null var x = 10; x = 3.5; x = "hello"; alert(x);

Operators Usual arithmetic operators +,-,*,/,% Convert strings to numbers using parse var num = parseInt(“100”); Var num2 = parseFloat(“3.5”); String concatenation with + and types converted automatically var myString = "hello" " there" ; alert(myString);

Booleans true and false defined We also have “truthy” values Numbers: 0 and NaN considered false, others considered true Strings: “” considered false, others considered true Null and Undefined considered false Undefined is the value assigned to a declared variable with no value Null is used to set an object to nothing

Determining the data type typeof variable returns the data type as a string var myString = "hello"; alert(typeof myString);// String

Defining functions function isEven(num)// Separate multiple parameters by, { if ((num % 2) == 0) return true; else return false; } alert(isEven(3)); alert(isEven(4));

Functions are First Class We can assign a function to a variable using a function expression; we will use a similar format when we create our own objects var myFunc = function(x) { var z = x; z++; return z; } alert(myFunc(10));// Outputs 11 var myFunc = function(x) { var z = x; z++; return z; } function foo(f) { alert(f(10)); } foo(myFunc); function addOne() { return function(x) { return x+1; } var f = addOne(); alert(f(10));

Operators Usual relational operators ==, !=,, = Also have Identity and Nonidentity === and !== True or false if values AND types match Logical Operators &&, ||, !

Control Statements if statement/ if else and switch statement the same as Java The ternary operator is also available (condition) ? (return value if condition true) : (return value if condition false) While loop, do-while and for loop also the same for (var i = 0; i < 100; i++)

Scoping Global scope for identifiers declared at the topmost level Local scope for identifiers declared inside a function Lexical/static scope for functions var x = 10; function foo1() { var x = 20; // Local variable x; foo2(); } function foo2() { alert("In foo 2 x= " + x); // Outputs 10 } alert("Outside: " + x); foo1(); foo2(); alert("Outside: " + x);

No Block Scope If a variable is declared in a block, it is added to the scope of that block (e.g. global or a function) and does not go out of scope when the block exits if (1 < 2) { var x = 10; } alert("Outside: " + x);// Legal and x = 10 here

Variable Declarations If a variable is assigned a value but no such variable exists then it is created with global scope function foo() { x = 10; } foo(); alert("Outside: " + x); function foo() { var x = 10; } foo(); alert("Outside: " + x);

Arrays Arrays are an object type var myArray = new Array(); var myArray = [];// Same thing var myArray = [1, 2, 3]; myArray.length // Access length of array myArray[0] = 3; alert(myArray[2]); myArray.push(val); // Add to end Useful array methods: concat(a1,a2) – concatenates arrays join(separator) – Converts to string pop() – Removes last element reverse() - Reverses array

String methods Strings also an object (along with Date, Number) Some methods: charAt(index) indexOf(searchString) replace(toReplace, replaceWith) split(separator) substr(startIndex, numChars) toLowerCase() toUpperCase() var myString = "10,20,30,40"; var myArray = myString.split(","); alert(myArray.join(":"));

Creating an object var myObj = new Object();// Calls the object constructor myObj.x = 10;// Creates properties on the fly myObj.y = 100; Can also use object literal notation: var myObj = { x : 10, y: 20 }; Objects can be returned or sent in as a parameter

Custom Objects Use a function that serves as a constructor with properties and sub- functions (methods) function Point(x, y) { this.x = x;// Public instance vars this.y = y; var d = 10;// Internal "private" variable this.getDistance = function(p)// A method { var x2 = Math.pow(p.x - this.x, 2); var y2 = Math.pow(p.y - this.y, 2); d = Math.sqrt(x2 + y2); return d; }; } var point1 = new Point(0,0); var point2 = new Point(1,1); var distance = point1.getDistance(point2); alert(distance);// 1.41 One flaw: has to create all of the Point items (x, y, d, getDistance) for every Point created OK for vars but not for getDistance

Prototype Object Avoid the problem on the previous slide with the prototype object Every function has a prototype property Change made to the prototype object is seen by all instances of that data type Similar to declaring something static; can create a member function once and assign it to the prototype property function Point(x, y) { this.x = x; this.y = y; } Point.prototype.getDistance = function(p) { var x2 = Math.pow(p.x - this.x, 2); var y2 = Math.pow(p.y - this.y, 2); return Math.sqrt(x2 + y2); }; var point1 = new Point(0,0); var point2 = new Point(1,1); var distance = point1.getDistance(point2); alert(distance); For “static” variables could use Point.val = 100;

Overriding the prototype Possible to override the prototype based on scoping rules function Point(x, y) { this.x = x;// Public instance vars this.y = y; } Point.prototype.getDistance = function(p) { var x2 = Math.pow(p.x - this.x, 2); var y2 = Math.pow(p.y - this.y, 2); return Math.sqrt(x2 + y2); }; var point1 = new Point(0,0); var point2 = new Point(1,1); point1.getDistance = function(p) { return 10; }; var distance = point1.getDistance(point2); alert(distance);

Inheritance Inheritance is possible using the call() method which invokes a method as though it were a method of the object specified by the first parameter function Person(first, last) { this.firstName = first; this.lastName = last; } Person.prototype.getFullName = function() { return this.firstName + " " + this.lastName; }; function Employee(first,last,position) { Person.call(this, first, last); this.position = position; } var eddie = new Employee("Eddie","Haskell","Schmoozer"); alert(eddie.firstName + " " + eddie.lastName + " " + eddie.position); But this doesn’t work yet: alert(eddie.getFullName() + " " + eddie.position);

Inheritance via the prototype We can chain an instance of a base type to the sub-type’s prototype and get inheritance as JavaScript searches through the chain for a matching method function Person(first, last) { this.firstName = first; this.lastName = last; } Person.prototype.getFullName = function() { return this.firstName + " " + this.lastName; }; function Employee(first,last,position) { Person.call(this, first, last); this.position = position; } Employee.prototype = new Person(); var eddie = new Employee("Eddie","Haskell","Schmoozer"); alert(eddie.getFullName() + " " + eddie.position);

Programming the Browser Already using alert() but more formally it is window.alert(message); Also: var name = prompt(“Enter your name”, “Default Text”); var result = confirm(“Ready to end class?”); // Shows OK/Cancel location.href = “ location.reload(true); var childwindow = open(“URL”,”Title”,”height=1024, width=400”);

Navigating the Document Object Model (DOM) Sample Some text More text Text with a span element document “Sample” Text Text Text Text Nodes are Document, Element, Attribute, Text

Navigating the Document Object Model (DOM) Sample Some text More text Text with a span element Several ways to access elements, one is by ID: var span = document.getElementById("foo"); var txt = span.childNodes[0].nodeValue; span.childNodes[0].nodeValue = "twist";

Creating Elements Creating a div (division or section) and text elements: var el = document.createElement("div"); el.id = "myDiv"; var text = document.createTextNode("Hello world!"); el.appendChild(text); el.setAttribute("align","center");// Set attributes document.body.appendChild(el); // Adds to end of the body

Adding multiple DIV’s var divs = Array(); var i; for (i = 0; i < 10; i++) { divs[i] = document.createElement("div"); divs[i].id = "myDiv" + i; var text = document.createTextNode("Hello world!"); divs[i].appendChild(text); divs[i].setAttribute("align","center");// Set attributes document.body.appendChild(divs[i]); // Adds to end of the body }

Can set properties using innerHTML var el = document.getElementById("foo"); el.innerHTML = " New Value "; Some say this is bad because it can lead to invalid markup

Accessing Forms Example: Javascript Demo Text with a span element var el = document.getElementById("text1"); el.value = parseInt(el.value) + 1;

Scripting Buttons – Click Event Javascript Demo Click Me function addOne() { var el = document.getElementById("text1"); el.value = parseInt(el.value) + 1; }