05 – Java Script (1) Informatics Department Parahyangan Catholic University.

Slides:



Advertisements
Similar presentations
Introducing JavaScript
Advertisements

JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
1 COMM 1213 H1 COMP 4923 X1 JavaScript 1 (Readings: Ch. 10, 11 Knuckles)
Javascript Introduction Norman White Material is from w3schools.com Go there to run examples interactively.
Lesson 12- Unit L Programming Web Pages with JavaScript.
The Web Warrior Guide to Web Design Technologies
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
2012 •••••••••••••••••••••••••••••••••• Summer WorkShop Mostafa Badr
Introduction to scripting
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
Database-Driven Web Sites, Second Edition1 Chapter 3 INTRODUCTION TO CLIENT-SIDE SCRIPTS.
DOM and JavaScript Aryo Pinandito.
JavaScript Introduction
CSS Class 7 Add JavaScript to your page Add event handlers Validate a form Open a new window Hide and show elements Swap images Debug JavaScript.
Week 9 PHP Cookies and Session Introduction to JavaScript.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
Javascript. Outline Introduction Fundamental of JavaScript Javascript events management DOM and Dynamic HTML (DHTML)
Lesson13. JavaScript JavaScript is an interpreted language, designed to function within a web browser. It can also be used on the server.
Introduction to JavaScript Gordon Tian
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
1 JavaScript in Context. Server-Side Programming.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 2: What is JavaScript?
Tutorial 10 Programming with JavaScript
Done by: Hanadi Muhsen1 Tutorial 1.  Learn the history of JavaScript  Create a script element  Write text to a Web page with JavaScript  Understand.
CHAPTER 4 Java Script อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
Introduction to JavaScript Basharat Mahmood, Department of Computer Science, CIIT, Islamabad, Pakistan. 1.
Introduction.  The scripting language most often used for client-side web development.  Influenced by many programming languages, easier for nonprogrammers.
JavaScript Introduction 1. What is Java Script ? JavaScript is a client-side scripting language. A scripting language is a lightweight programming language.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
JavaScript, jQuery, and Mashups Incorporating JavaScript, jQuery, and other Mashups into existing pages.
Dr. Qusai Abuein1 Internet & WWW How to program Chap.(6) JavaScript:Introduction to Scripting.
JavaScript - Basic Concepts Prepared and Presented by Hienvinh Nguyen, Afshin Tiraie.
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
Scott Marino MSMIS Summer Session Web Site Design and Authoring Session 8 Scott Marino.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
CSC318 – DYNAMIC WEB APPLICATION DEVELOPMENT BY: SITI NURBAYA ISMAIL FACULTY of COMPUTER and MATHEMATICAL SCIENCES.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
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.
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
1 JavaScript in Context. Server-Side Programming.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
This is our seminar JavaScript And DOM This is our seminar JavaScript And DOM.
Javascript Basic Concepts Presentation By: Er. Sunny Chanday Lecturer CSE/IT RBIENT.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
JAVASCRIPT A quick review. True False ■The DOM is a standardized way of referring to parts of a Web page. ■TRUE ■In the DOM, attributes have their own.
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.
Javascript Prof. Wenwen Li School of Geographical Sciences and Urban Planning 5644 Coor Hall
SE-2840 Dr. Mark L. Hornick 1 Dynamic HTML Handling events from DOM objects.
Java Script Introduction. Java Script Introduction.
Exploring JavaScript Ch 14
JavaScript Syntax and Semantics
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
14 A Brief Look at JavaScript and jQuery.
JavaScript an introduction.
Web Systems Development (CSC-215)
Objectives Insert a script element Write JavaScript comments
Your 1st Programming Assignment
WEB PROGRAMMING JavaScript.
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
Functions, Regular expressions and Events
JavaScript CS 4640 Programming Languages for Web Applications
Web Programming and Design
Web Programming and Design
JavaScript CS 4640 Programming Languages for Web Applications
Presentation transcript:

05 – Java Script (1) Informatics Department Parahyangan Catholic University

 JavaScript is the programming language that adds interactivity and custom behaviors to our sites.  It is a client-side scripting language (runs on the user’s machine and not on the server).

 JavaScript has nothing to do with Java.  It was created by Brendan Eich at Netscape in 1995 and originally named “LiveScript”.  For the sake of marketing, “LiveScript” became “JavaScript”.

 Java Script is placed inside … tag  Example: Hello World document.write("Hello World") Your browser doesn't support or has disabled JavaScript

 JS can be placed anywhere within HTML document  It is usually placed inside section  to make sure critical functions are ready to use by other scripts in the document  so that JS can writes meta tag into the section (because the location of your script is the part of the document it writes to by default)

 We can include files of JavaScript code either from our own website or from anywhere on the Internet.  Example:

 Web browsers provide a JavaScript console as part of their developer tools.  Keyboard shortcut for opening console (Win):  Chrome: Ctrl + Shift + J  Firefox: Ctrl + Shift + K  Internet Explorer: F12 then click on “Console” tab  Safari: Ctrl + Shift + C (need to enable the “Show Develop menu in menu bar” setting in “Advanced” preferences pane)

 According to the TIOBE index, Java, Java Script, and PHP are all heavily influenced by the C Programming Language, thus they all share many similarities.

 Single-line comment: // This is a comment  Multiline comments: /* This is a section of multiline comments that will not be interpreted */

 JavaScript generally does not require semicolons for one statement on a line.  The following is valid: x += 10  However, more than one statement on a line needs to be separated with semicolons: x += 10; y -= 5; z = 0 In doubt, use a semicolon.

 Fixed values (called literals)  Numbers : written with or without decimals  String: written within double or single quotes "John Doe" 'John Doe'  Variable values (called variables)  JavaScript has dynamic types. This means that the same variable can be used as different types.

 Use var keyword to create a variable. Example: var x;  Naming rules:  the first character must be a letter, an underscore (_), or a dollar sign ($).  Subsequent characters may be letters, digits, underscores, or dollar signs.  All JavaScript identifiers are case sensitive

 Global variables are ones defined outside of any functions (or within functions, but defined without the var keyword).  Example: function test() { a = 123 // Global scope var b = 456 // Local scope if (a == 123) var c = 789 // Local scope }

var x; // Now x is undefined var x = 5; // Now x is a Number var x = "John"; // Now x is a String What is the value of x ? var x = 16 + "Volvo"; var x = "Volvo"; var x = "Volvo" ;

n = "123" n *= 1 // Convert 'n' into a number n = 123 n += "" // Convert 'n' into a string If n cannot be converted to number, it returns NaN value

 The parseInt() function parses a string and returns the first integer found. It returns NaN if the string doesn’t start with a number.  Leading and trailing spaces are allowed.  Syntax: parseInt(string,radix)  The radix parameter is optional and is used to specify which numeral system to be used.

 Example: returns parseInt("10.00") parseInt("10.33") parseInt(" ") parseInt("40 years") parseInt("He was 40") parseInt("10",8) parseInt("0x10") parseInt("10",16) NaN 8 16

 The parseFloat() function parses a string and returns the first floating point number found. It returns NaN if the string doesn’t start with a number.  Leading and trailing spaces are allowed.  Syntax: parseFloat(string)

 Example: returns parseFloat("10.00") parseFloat("10.33") parseFloat(" ") parseFloat(" 60 ") parseFloat("40 years") parseFloat("He was 40") NaN

 An array can contain string or numeric data, as well as other arrays.  To assign values to an array, use the following syntax (which in this case creates an array of strings): toys = ['bat', 'ball', 'whistle', 'puzzle', 'doll']

 To create a multidimensional array, nest smaller arrays within a larger one. Example: face = [['R','G','Y'], ['W','R','O'], ['Y','W','G']] or: top = ['R', 'G', 'Y'] mid = ['W', 'R', 'O'] bot = ['Y', 'W', 'G'] face = [top, mid, bot]  Array index starts from zero. Example: to access letter ‘O’, use face[1][2].

 Arithmetic:  Addition +  Subtraction -  Multiplication *  Division /  Modulus %  Increment ++  Decrement -- floating point division means +1 means -1

 Assignment:  =  +=  -=  *=  /=  %=  Logical:  && And  || Or  ! Not

 Comparison:  == equal to  != not equal  > greater than  >= greater than or equal to  < less than  <= less than or equal to  === equal to (and of the same type)  !== not equal to (and of the same type) (10 == '10') is true (10 === '10') is false (10 == '10') is true (10 === '10') is false (10 != '10') is false (10 !== '10') is true (10 != '10') is false (10 !== '10') is true

 This breaks down the parts of an HTML document into discrete objects, each with its own properties and methods and each subject to JavaScript’s control.  The HTML DOM is a standard for how to get, change, add, or delete HTML elements

 Example: Link Test Click me url = document.links.mylink.href document.write('The URL is ' + url)

 We can use getElementById() method to fetch an element by its id Link Test Click me url = document.getElementById("mylink").href document.write('The URL is ' + url)

 Several ways to find HTML element(s):  by id var x = document.getElementById("intro");  by tag name var x = document.getElementById("main"); var y = x.getElementsByTagName("p");  by class name document.getElementsByClassName("intro");

 Several ways to find HTML element(s):  by HTML object collections This example finds the form element with id="frm1", in the forms collection, and displays all element values: var x = document.getElementById("frm1"); var text = ""; var i; for (i = 0; i "; }

 To modify the content of an HTML element, use the innerHTML property  To change the value of an HTML attribute, use this syntax: document.getElementById(id).attribute=new value document.getElementById("p1").innerHTML = "New text!"; document.getElementById("myImage").src="landscape.jpg";

 To change the style of an HTML element, use this syntax: document.getElementById(id).style.property=new style document.getElementById("p2").style.color = "blue";

 The general syntax for a function is: function function_name([parameter [,...]]) { statements } function displayItems(v1, v2, v3, v4, v5){ document.write(v1 + " "); document.write(v2 + " "); document.write(v3 + " "); document.write(v4 + " "); document.write(v5 + " "); } displayItems("Dog", "Cat", "Pony", "Hamster", "Tortoise");

 The arguments array gives you the flexibility to handle a variable number of arguments function displayItems(){ for(j=0 ; j<displayItems.arguments.length ; ++j) document.write(displayItems.arguments[j] + " "); }

 Some examples of HTML events:  An HTML web page has finished loading  An HTML input field was changed  An HTML button was clicked  JavaScript lets you execute code when events are detected.

 HTML allows event handler attributes, with JavaScript code, to be added to HTML elements.  Syntax:

 onclick The user clicks an HTML element  onchange An HTML element has been changed  onmouseover The user moves the mouse over an HTML element  onmouseout The user moves the mouse away from an HTML element  onkeydown / onkeyup The user pushes / releases a keyboard key  onload The browser has finished loading the page

 Example: The time is?

 Example: The time is?

 Example: Click the button to display the date. The time is? function displayDate() { document.getElementById("demo").innerHTML = Date(); }

 Example: <div onmouseover="mOver(this)" onmouseout="mOut(this)" style="background-color:#D94A38;width:120px;"> Mouse Over Me function mOver(obj) { obj.innerHTML = "Thank You" } function mOut(obj) { obj.innerHTML = "Mouse Over Me" }