a Why JavaScript? jonkv interactivity on the web CGI JavaScript Java Applets Netscape LiveScript JavaScript 1: Example

Similar documents
CITS1231 Web Technologies. JavaScript Math, String, Array, Number, Debugging

a Why JavaScript? jonkv interactivity on the web CGI JavaScript Java Applets Netscape LiveScript JavaScript 1: Example

What Is JavaScript? A scripting language based on an object-orientated programming philosophy.

Javascript Methods. concat Method (Array) concat Method (String) charat Method (String)

PES DEGREE COLLEGE BANGALORE SOUTH CAMPUS 1 K.M. before Electronic City, Bangalore WEB PROGRAMMING Solution Set II

COMS 469: Interactive Media II

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

Client-Side Web Technologies. JavaScript Part I

Chapter 1 Introduction to Computers and the Internet

Chapter 4 Basics of JavaScript

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

JAVASCRIPT BASICS. JavaScript String Functions. Here is the basic condition you have to follow. If you start a string with

JavaScript CS 4640 Programming Languages for Web Applications

Lecture 3: The Basics of JavaScript. Background. Needs for Programming Capability. Origin of JavaScript. Using Client-side JavaScript

CSC Web Programming. Introduction to JavaScript

5. Strict mode use strict ; 6. Statement without semicolon, with semicolon 7. Keywords 8. Variables var keyword and global scope variable 9.

JavaScript CS 4640 Programming Languages for Web Applications

CGS 3066: Spring 2015 JavaScript Reference

About the Tutorial. Audience. Prerequisites. Copyright and Disclaimer

Introduction to JavaScript p. 1 JavaScript Myths p. 2 Versions of JavaScript p. 2 Client-Side JavaScript p. 3 JavaScript in Other Contexts p.

5. JavaScript Basics

Javascript Hierarchy Objects Object Properties Methods Event Handlers. onload onunload onblur onfocus

PIC 40A. Lecture 10: JS: Wrapper objects, Input and Output, Control structures, random numbers. Copyright 2011 Jukka Virtanen UCLA 1 04/24/17

This course is designed for web developers that want to learn HTML5, CSS3, JavaScript and jquery.

Like most objects, String objects need to be created before they can be used. To create a String object, we can write

JAVASCRIPT. Ajax Technology in Web Programming. Sergio Luján Mora. DLSI - Universidad de Alicante 1. Basic syntax and features

Basics of JavaScript. Last Week. Needs for Programming Capability. Browser as Development Platform. Using Client-side JavaScript. Origin of JavaScript

UNIT-4 JAVASCRIPT. Prequisite HTML/XHTML. Origins

JavaScript: Introduction, Types

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

CSc 337 LECTURE 5: GRID LAYOUT AND JAVASCRIPT

UNIT -II. Language-History and Versions Introduction JavaScript in Perspective-

JavaScript Basics. Mendel Rosenblum. CS142 Lecture Notes - JavaScript Basics

JavaScript Introduction

ID1354 Internet Applications

JavaScript I VP R 1. Copyright 2006 Haim Levkowitz. Outline

710 Index Attributes, 127 action attribute, 263 assigning, bottom attribute, domain name attribute, 481 expiration date attribute, 480 8

Objectives. Introduction to JavaScript. Introduction to JavaScript INFS Peter Y. Wu, RMU 1

INFS 2150 Introduction to Web Development and e-commerce Technology. Programming with JavaScript

More on new. Today s Goals. CSCI 2910 Client/Server-Side Programming. Creating/Defining Objects. Creating/Defining Objects (continued)

Such JavaScript Very Wow

The JavaScript Language

Programming language components

A.A. 2008/09. Why introduce JavaScript. G. Cecchetti Internet Software Technologies

5/23/2015. Core Java Syllabus. VikRam ShaRma

Working with JavaScript

JavaScript: More Syntax and Using Events

The course is supplemented by numerous hands-on labs that help attendees reinforce their theoretical knowledge of the learned material.

JavaScript: The Basics

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

CIW 1D CIW JavaScript Specialist.

The first sample. What is JavaScript?

COMS 469: Interactive Media II

JavaScript. What s wrong with JavaScript?

JavaScript Basics. The Big Picture

CHAPTER 6 JAVASCRIPT PART 1

<form>. input elements. </form>

E ECMAScript, 21 elements collection, HTML, 30 31, 31. Index 161

CERTIFICATE IN WEB PROGRAMMING

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Introduction to Programming Using Java (98-388)

Outline. Introduction to JavaScript Resources What is JavaScript? JavaScript in web pages

Try the following example using the Try it option available at the top right corner of the below sample code box

3 The Building Blocks: Data Types, Literals, and Variables

Lecture 8 (7.5?): Javascript

CS1520 Recitation Week 2

Web Application Development

INTRODUCTION TO WEB DEVELOPMENT AND HTML. Lecture 15: JavaScript loops, Objects, Events - Spring 2011

URLs and web servers. Server side basics. URLs and web servers (cont.) URLs and web servers (cont.) Usually when you type a URL in your browser:

JavaScript by Vetri. Creating a Programmable Web Page

4.1 Overview of JavaScript

Lesson 9: Custom JavaScript Objects

JavaScript Specialist v2.0 Exam 1D0-735

CITS1231 Web Technologies. JavaScript

SEEM4570 System Design and Implementation Lecture 03 JavaScript

So, if you receive data from a server, in JSON format, you can use it like any other JavaScript object.

Javascript. UNIVERSITY OF MASSACHUSETTS AMHERST CMPSCI 120 Fall 2010

DC71 INTERNET APPLICATIONS JUNE 2013

Background. Javascript is not related to Java in anyway other than trying to get some free publicity

Enterprise Content Management System Monitor 5.2

Web Design and Application Development

CISC 1600 Lecture 2.4 Introduction to JavaScript

5/19/2015. Objectives. JavaScript, Sixth Edition. Introduction to the World Wide Web (cont d.) Introduction to the World Wide Web

Index COPYRIGHTED MATERIAL

CECS 189D EXAMINATION #1

COMS W3101: SCRIPTING LANGUAGES: JAVASCRIPT (FALL 2017)

Darshan Institute of Engineering & Technology for Diploma Studies Unit 2. 2 Working with JavaScript

COMS 469: Interactive Media II

Manipulating Data in Strings and Arrays

JavaScript By: A. Mousavi & P. Broomhead SERG, School of Engineering Design, Brunel University, UK

The JavaScript Language

JAVASCRIPT. sarojpandey.com.np/iroz. JavaScript

Documents and computation. Introduction to JavaScript. JavaScript vs. Java Applet. Myths. JavaScript. Standard

COMP284 Scripting Languages Lecture 14: JavaScript (Part 1) Handouts

Web Site Development with HTML/JavaScrip

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

JavaScript Lecture 1

JavaScript. Training Offer for JavaScript Introduction JavaScript. JavaScript Objects

JavaScript s role on the Web

Java Overview An introduction to the Java Programming Language

Transcription:

Why JavaScript? 2 JavaScript JavaScript the language Web page manipulation with JavaScript and the DOM 1994 1995: Wanted interactivity on the web Server side interactivity: CGI Common Gateway Interface Batch oriented (fill in a form, get interactive results) "Heavy weight" client side interactivity: Java Applets Self contained part of a web page Interacts with the user + possibly a server Does not interact with the document itself (Interactive versions of Macromedia Flash not available until 1998) "Light weight" client side interactivity: Netscape LiveScript Script language for the web (introduced late 1995) Renamed to JavaScript (marketing!) Standardized version: ECMAscript (ECMA 262, ISO 16262) Also used in Flash, Firefox/Thunderbird UI, PDF files, MacOS Dashboard Widgets, PhotoShop, JavaScript 1: Example 3 JavaScript 2: Script files 4 A simple JavaScript example: In HTML, scripts are CDATA not parsed by browsers, validators <!DOCTYPE HTML PUBLIC > <HTML> <head> <title>hello, World!</title> <script type="text/javascript"> alert("hello, World!"); // Show an alert to the user </script> </head><body> </body></html> In XHTML, your inline code is by default PCDATA (parsed) if (a < b) the < will be interpreted as the start of a tag <script type="text/javascript"> /* <![CDATA[ */ your code /* ]]> */ </script> Better solution: Separate script file Test.html: <!DOCTYPE HTML PUBLIC > <HTML> <head> <title>hello, World!</title> </head> <body> <script type="text/javascript" src="example.js"></script> </body> </HTML> example.js: // Show an alert to the user alert("hello, World!");

JavaScript 3: Types and type checking 5 JavaScript 4: Control 6 Like many script languages: No static type checking Variables are always declared with "var", not with a type var myvar = "Testing"; myvar = 10; myvar = true; Example: Some calculations var secs_per_min = 60; var mins_per_hour = 60; var hours_per_day = 24; var secs_per_day = secs_per_min * mins_per_hour * hours_per_day; Special values: null (behaves like 0 in numeric context, false in boolean context) undefined (behaves like false in boolean context) Control structures are similar to Java if (condition) { else { switch / case for (var i= 0; i < 10; i++) { while (condition) { do { while (condition) break / continue try { throw new ; catch (e) { finally { try { throw "Something"; catch (e) { finally { // Comment /* Long comment */ JavaScript 5: Operators 7 JavaScript 6: Functions 8 Operators are similar to Java, C, C++ ==!= // equals (tries to convert types before comparison) ===!== // (strict equals no type conversion) + * / < > << >> >>>// left shift, right shift, unsigned right shift += = *= /= ++, &&, Functions: Declared using the "function" keyword Late type checking: Don't declare parameter types function get_secs(days) { var secs_per_min = 60; var mins_per_hour = 60; var hours_per_day = 24; var secs_per_day = secs_per_min * mins_per_hour * hours_per_day; return days * secs_per_day; var secs_per_week = get_secs(7); Checking whether functions exist: if (get_secs) { Important! Used to check the level of JavaScript support. Important! Used to check the level of DOM support.

JavaScript 7: Object-oriented 9 JavaScript 8: Constructors 10 Object oriented but not class based Can use constructors if you want to Individual objects can have their own methods or fields Names the type ("Person") var me = new Object(); me.name = "Jonas Kvarnström"; // Creates a "name" field me.phone = null; me.getname = function() { return this.name; var you = new Object(); you.name = "Jane Doe"; you.address = "somewhere"; Literal notation: function Person(name, phone) { this.name = name; this.phone = phone; this.getname = function() { return this.name; var me = new Person("Jonas Kvarnström", "2305"); Can still add methods / fields incrementally var me = { name: "Jonas Kvarnström", phone: null, getname: function() { return this.name; ; Only weak syntax checking possible me.address = "Somewhere"; me.setaddress = function(addr) { this.address = addr; alert(me.phone); // OK alert(you.phone); // No such property (runtime error) JavaScript 9: Adding new methods 11 JavaScript 10: Adding to prototype 12 Inheritance based on prototypes Each object inherits properties from a prototype object function Employee(name, phone, department) { this.name = name; this.phone = phone; this.department = department; var me = new Employee("Jonas", "2305", "IDA"); me.prototype = new Person(); // Inheritance on a per object level! me.getphone(); // works inherited function from prototype Set the prototype of a constructor method Default prototype for objects constructed using Employee function Employee.prototype = new Person(); // "extends Person" Can add properties to the prototype at any time! Incremental extension person.prototype.getphone = function() { return phone; Now all "person" objects have this method Works for built in classes too! if (!String.prototype.trim) { // If trim does not exist String.prototype.trim = function() { return this.replace(/^\s+/,"").replace(/\s+$/,"");

JavaScript 11: Arrays 13 JavaScript 12: Associative arrays 14 Arrays: Kind of like Java arrays Allocated with new operator, or [] syntax var names1 = new Array("Roger", "Moore"); var names2 = [ "Roger", "Moore" ]; var empty = new Array(10); Indexed using [] syntax names[0] == "Roger" empty[0] == undefined Have a.length property Example: var reply = prompt("what's your name?", ""); var names = reply.split(" "); for (var pos = 0; pos < names.length; pos++) { alert("one of your names is " + names[pos]); Useful array methods concat joins two arrays join joins array elements into string push(x), pop() slice() returns a subsection of the array splice() adds/removes elements sort() Arrays can be associative Can replace Map / HashMap in Java var phonenumbers = new Array(); phonenumbers["jonas"] = "070 xxxxxxx"; phonenumbers[ Martin"] = "070 xxxxxxx"; Can iterate over all keys for (var name in phonenumbers) { // Jonas, Martin, dosomethingwith(phonenumbers[name]); Properties and arrays: Different interfaces, same data structure var me = new Person("Jonas Kvarnström", null); me["name"] = "Jonas Kvarnström"; for (var prop in me) { /* iterate over "name", "phone", "getname" */ JavaScript 13: Strings 15 JavaScript 14: Dates 16 String Single or double quotes var myvar = 'asdf', othervar = "qwerty"; Fields: length General Methods charat, charcodeat, indexof, lastindexof fromcharcode // from sequence of Unicode values "abc def ghi".split(" ") == array containing "abc", "def", "ghi" substring, substr, slice, concat match, replace, search // regular expressions tolowercase, touppercase HTML related Methods anchor, link // HTML named anchor / hyperlink big, blink, bold, fixed, italics, small, strike, sub, sup Date Constructors var date = new Date(); // today var date = new Date("December 31, 2010 12:34:56"); var date = new Date(2010, 12, 31); Methods getyear(), getmonth(), getdate, getday() // and setters gethour(), getminute(), getsecond() // and setters getfullyear() // four digit year

JavaScript 15: History and Window 17 Browser specific JavaScript objects: history the page history history.back(), history.forward() history.go(delta) // 1 = back, 2 = 2 steps back,... history.go('www.ida.liu.se') // nearest URL containing 'www.ida.liu.se' window.location the current location URL window.location = "http://www.ida.liu.se"; // New history entry window.location.reload(); window.location.replace("http://www.ida.liu.se"); // No new entry