JavaScript Introduction

Slides:



Advertisements
Similar presentations
Introducing JavaScript
Advertisements

JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
Javascript Introduction Norman White Material is from w3schools.com Go there to run examples interactively.
The Web Warrior Guide to Web Design Technologies
1 Outline 13.1Introduction 13.2A Simple Program: Printing a Line of Text in a Web Page 13.3Another JavaScript Program: Adding Integers 13.4Memory Concepts.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Tutorial 10 Programming with JavaScript
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
XP Tutorial 1 New Perspectives on JavaScript, Comprehensive1 Introducing JavaScript Hiding Addresses from Spammers.
2012 •••••••••••••••••••••••••••••••••• Summer WorkShop Mostafa Badr
Introduction to scripting
JavaScript CMPT 281. Outline Introduction to JavaScript Resources What is JavaScript? JavaScript in web pages.
4.1 JavaScript Introduction
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
Lectured By: Vivek Dimri Asst. Professor, SET, Sharda University
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
Database-Driven Web Sites, Second Edition1 Chapter 3 INTRODUCTION TO CLIENT-SIDE SCRIPTS.
1 CLIENT-SIDE SCRIPTS. Objectives 2 Learn how to reference objects in HTML documents using the HTML DOM and dot syntax Learn how to create client-side.
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)
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
1 JavaScript in Context. Server-Side Programming.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 2: What is JavaScript?
Tutorial 10 Programming with JavaScript. XP Objectives Learn the history of JavaScript Create a script element Understand basic JavaScript syntax Write.
Tutorial 10 Programming with JavaScript
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
JavaScript Introduction 1. What is Java Script ? JavaScript is a client-side scripting language. A scripting language is a lightweight programming language.
JavaScript, jQuery, and Mashups Incorporating JavaScript, jQuery, and other Mashups into existing pages.
XP Tutorial 10New Perspectives on HTML and XHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial.
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
05 – Java Script (1) Informatics Department Parahyangan Catholic University.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2000 Deitel & Associates, Inc. All rights reserved. Outline 8.1Introduction 8.2A Simple Program: Printing a Line of Text in a Web Page 8.3Another JavaScript.
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.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
1 JavaScript in Context. Server-Side Programming.
Java Script About Java Script Document Object Model Incorporating JavaScript Adding JavaScript to HTML Embedding a Javascript External Scripts Javascript.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
Javascript Overview. What is Javascript? May be one of the most popular programming languages ever Runs in the browser, not on the server All modern browsers.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
Javascript Basic Concepts Presentation By: Er. Sunny Chanday Lecturer CSE/IT RBIENT.
CGS 3066: Web Programming and Design Spring 2016 Introduction to JavaScript.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
JavaScript and AJAX 2nd Edition Tutorial 1 Programming with JavaScript.
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
JavaScript Tutorial First lecture 19/2/2016. Javascript is a dynamic computer programming language. It is lightweight and most commonly used as a part.
Introduction to.
Java Script Introduction. Java Script Introduction.
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Chapter 6 JavaScript: Introduction to Scripting
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
14 A Brief Look at JavaScript and jQuery.
WEB PROGRAMMING JavaScript.
Tutorial 10 Programming with JavaScript
JavaScript CS 4640 Programming Languages for Web Applications
Tutorial 10: Programming with javascript
JavaScript: Introduction to Scripting
Web Programming and Design
JavaScript CS 4640 Programming Languages for Web Applications
Presentation transcript:

JavaScript Introduction

What is Java Script ? JavaScript is a client-side scripting language. A scripting language is a lightweight programming language. JavaScript is programming code that can be inserted into HTML pages. JavaScript inserted into HTML pages, can be executed by all modern web browsers. Java Script can enhance the dynamics and interactive features of your page by allowing you to perform calculations, check forms, write interactive games, add special effects, customize graphics selections, create security passwords and more. JavaScript Introduction

What is Java Script ? JavaScript is used in Web site development to such things as: check or modify the contents of forms change images open new windows and write dynamic page content. to make DHTML by CSS. This allows you to make parts of your web pages appear or disappear or move around on the page JavaScript Introduction

Java Script Vs Java JavaScript Java Interpreted (not compiled) by client. Compiled on server before execution on client. Object-based. Code uses built-in, extensible objects, but no classes or inheritance. Object-oriented. Applets consist of object classes with inheritance. Code integrated with, and embedded in, HTML. Applets distinct from HTML (accessed from HTML pages). Variable data types not declared (loose typing). Variable data types must be declared (strong typing). Secure. Cannot write to hard disk. JavaScript Introduction

ECMA Script The responsibility for the development of a scripting standard has been transferred to an international body called the European Computer Manufacturers Association (ECMA). The standard developed by the ECMA is called ECMAScript, though browsers still refer to it as JavaScript. The latest version is ECMA-262, which is supported by the major browsers. JavaScript Introduction

Writing a Java Script Program The Web browser runs a JavaScript program when the Web page is first loaded, or in response to an event. JavaScript programs can either be placed directly into the HTML file or they can be saved in external files. placing a program in an external file allows you to hide the program code from the user source code placed directly in the HTML file can be viewed by anyone JavaScript Introduction

Writing a Java Script Program A JavaScript program can be placed anywhere within the HTML file. Many programmers favor placing their programs between <head> tags in order to separate the programming code from the Web page content and layout. Some programmers prefer placing programs within the <Body> of the Web page at the location where the program output is generated and displayed. JavaScript Introduction

How to use/implement Java Script? We can implement Java script in our web page by following three ways- Inside the head tag Within the body tag In an external file (with extension .js) JavaScript Introduction

Implementing Java Script Inside HEAD Tag: Syntax: <HTML> <HEAD> <SCRIPT TYPE= “TEXT/JAVASCRIPT”> <!- - Java Script Code // - -> </SCRIPT> </HEAD> <BODY> </BODY> </HTML> JavaScript Introduction

Implementing Java Script Within BODY Tag: Syntax: <HTML> <HEAD> </HEAD> <BODY> <SCRIPT TYPE= “TEXT/JAVASCRIPT”> <!- - java script code // - -> </SCRIPT> </BODY> </HTML> JavaScript Introduction

Implementing Java Script In an External Link: Syntax: <HTML> <HEAD> <SCRIPT SRC= “myscript.js”> </SCRIPT> </HEAD> <BODY> <input TYPE=“Button” onclick=“msg()” value=“Message”> </BODY> </HTML> Myscript.js: Function msg() { alert("Hello") } JavaScript Introduction

Java Script Syntax Issue JavaScript commands and names are case sensitive. JavaScript command lines end with a semicolon to separate it from the next command line in the program. in some situations, the semicolon is optional semicolons are useful to make your code easier to follow and interpret JavaScript Introduction

Displaying some text on web Page You can write on page by using following statement of Java script- document.write(“message”) document.writeln(“message”) Example: document.write(“<h1><B>My first message</B></h1>” ) ; JavaScript Introduction

JavaScript Display Possibilities JavaScript does NOT have any built-in print or display functions. JavaScript can "display" data in different ways: Writing into an alert box, using window.alert(). Writing into the HTML output using document.write(). Writing into an HTML element, using innerHTML. Writing into the browser console, using console.log(). JavaScript Introduction

window.alert() <!DOCTYPE html> <html> <body> <h1>My First Web Page</h1> <p>My first paragraph.</p> <script> window.alert(5 + 6); </script> </body></html> JavaScript Introduction

JavaScript Introduction

document.write() <html> <body> <h1>My First Web Page</h1> <p>My first paragraph.</p> <button type="button" onclick="document.write(5 + 6)"> Try it</button> </body> </html> JavaScript Introduction

JavaScript Introduction

innerHTML <html> <body> <h1>My First Web Page</h1> <p>My First Paragraph.</p> <p id="demo"></p> <script> document.getElementById("demo") .innerHTML = 5 + 6; </script> </body> </html> JavaScript Introduction

JavaScript Introduction

console.log() <!DOCTYPE html> <html><body> <h1>My First Web Page</h1> <p>My first paragraph.</p> <p> Activate debugging in your browser (Chrome, IE, Firefox) with F12, and select "Console" in the debugger menu. </p> <script> console.log(5 + 6); </script> </body> </html> JavaScript Introduction

JavaScript Introduction

Working with Variables & Data A variable is a named element in a program that stores information. var var_name; The following restrictions apply to variable names: the first character must be either a letter or an underscore character ( _ ) the remaining characters can be letters, numbers, or underscore characters variable names cannot contain spaces Variable names are case-sensitive. JavaScript Introduction

Types of Variables JavaScript supports four different types of variables Numeric variables can be a number, such as 13, 22.5, or -3.14159 string variables is any group of characters, such as “Hello” or “Happy Holidays!” Boolean variables are variables that accept one of two values, either true or false null variables is a variable that has no value at all JavaScript Introduction

Declaring/Creating a Variable Before you can use a variable in your program, you need to declare a variable using the var command or by assigning the variable a value. Any of the following commands is a legitimate way of creating a variable named “Month”: var Month; Month = “December”; var Month = “December”; JavaScript Introduction

Operators in JavaScript Following operators are used in JavaScript- Arithmetic Operator Comparison Operator JavaScript Introduction

Operators in JavaScript Following operators are used in JavaScript- Arithmetic Operator Operator Meaning Example + Addition 2 + 4 - Subtraction 6 - 2 * Multiplication 5 * 3 / Division 15 / 3 % Modulus 43 % 10 JavaScript Introduction

Example <body> <script type="text/JavaScript"> <!– var two = 2 var ten = 10 var linebreak = "<br />" document.write("two plus ten = ") var result = two + ten document.write(result) document.write(linebreak) document.write("ten * ten = ") result = ten * ten document.write("ten / two = ") result = ten / two //--> </script> </body> JavaScript Introduction

Output two plus ten = 12 ten * ten = 100 ten / two = 5 JavaScript Introduction

Operators in JavaScript Comparison Operator Operator Meaning Example Result == Equal To x == y false === Equal value and equal type x===y true != Not Equal To x != y !== not equal value or not equal type x!== < Less Than x < y > Greater Than x > y <= Less Than or Equal To x <= y >= Greater Than or Equal To x >= y JavaScript Introduction

Built-in JavaScript Functions Functions provided by the language and you cannot change them to suit your needs. Some of the built-in functions in JavaScript are shown here: eval - eval(expr) eval evaluates the expression or statements isFinite Determines if a number is finite isNaN Determines whether a value is “Not a Number” parseInt Converts string literals to integers, no number  NaN. parseFloat Finds a floating-point value at the beginning of a string. JavaScript Introduction

JavaScript Functions A function is a block of organized reusable code (a set of statements) for performing a single or related action. Begins with keyword “function” and the function name and “( … )” Inside the parentheses We can pass parameters to the function E.g. function myfuc(arg1, arg2) {…} Built-in and user-defined functions JavaScript Introduction

Creating JavaScript Functions Declaration Syntax Functions are declared using the function reserved word The return value is not declared, nor are the types of the arguments function function_name(parameters) { JavaScript commands } parameters are the values sent to the function (note: not all functions require parameters) { and } are used to mark the beginning and end of the commands in the function. JavaScript Introduction

Creating JavaScript Functions: Eg <HTML> <HEAD> <SCRIPT TYPE=TEXT/JAVASCRIPT> <!- - function myMessage() { document.write(“<B>Hello World”); } // - -> </SCRIPT> </HEAD> <BODY> <INPUT TYPE=BUTTON onClick=myMessage() Value=Click > </BODY> </HTML> JavaScript Introduction

Event in JavaScript JavaScript is its ability to help you create dynamic web pages that increase user interaction The building blocks of an interactive web page is the JavaScript event system. An event in JavaScript is something that happens with or on the webpage. A few example of events: A mouse click The webpage loading Mousing over a hot spot on the webpage, also known as hovering Selecting an input box in an HTML form A keystroke JavaScript Introduction

Event in JavaScript JS Event Handler Onclick onMouseOver onMouseOut onLoad onkeydown JavaScript Introduction

JavaScript Object Object Number String Date Array Boolean Math, etc JavaScript Introduction

What is the DOM? The DOM is a W3C (World Wide Web Consortium) standard. The DOM defines a standard for accessing documents: "The W3C Document Object Model (DOM) is a platform and language-neutral interface that allows programs and scripts to dynamically access and update the content, structure, and style of a document." The W3C DOM standard is separated into 3 different parts: Core DOM - standard model for all document types XML DOM - standard model for XML documents HTML DOM - standard model for HTML documents JavaScript Introduction

What is the HTML DOM? The HTML DOM is a standard object model and programming interface for HTML. It defines: The HTML elements as objects The properties of all HTML elements The methods to access all HTML elements The events for all HTML elements In other words: The HTML DOM is a standard for how to get, change, add, or delete HTML elements. JavaScript Introduction

What is the HTML DOM? When a web page is loaded, the browser creates a Document Object Model of the page. The HTML DOM model is constructed as a tree of Objects: JavaScript Introduction

JavaScript can change all the HTML elements in the page With the object model, JavaScript gets all the power it needs to create dynamic HTML: 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 JavaScript Introduction

The HTML DOM Document HTML DOM object model, the document object represents your web page. The document object is the owner of all other objects in your web page. If you want to access objects in an HTML page, you always start with accessing the document object. JavaScript Introduction

Finding HTML Elements Method Description document.getElementById() Find an element by element id document.getElementsByTagName() Find elements by tag name document.getElementsByClassName() Find elements by class name JavaScript Introduction

Changing HTML Elements Method Description element.innerHTML= Change the inner HTML of an element element.attribute= Change the attribute of an HTML element element.setAttribute(attribute,value) element.style.property= Change the style of an HTML element JavaScript Introduction

Adding and Deleting Elements Method Description document.createElement() Create an HTML element document.removeChild() Remove an HTML element document.appendChild() Add an HTML element document.replaceChild() Replace an HTML element document.write(text) Write into the HTML output stream JavaScript Introduction

Adding Events Handlers Method Description document.getElementById(id).onclick=function() {code} Adding event handler code to an onclick event JavaScript Introduction

Window Document Object The Window object represents a web browser window. In client-side JavaScript, the Window object is the global object that defines all top-level properties and methods. The properties and methods of the Window object are therefore global properties and global functions and you can refer to them by their property names without any object prefix. One of the properties of the Window object is named window and refers back to the Window object itself: window //The global Window object window.document //The document property of the window document //Or omit the object prefix JavaScript Introduction

Browser Document Object JavaScript Introduction

Document Object Java script enable browsers are capable of recognizing individual objects in a HTML page. Each HTML document loaded into a browser window becomes a Document object. The Document object provides access to all HTML elements in a page, from within a script. DOM has following properties- Anchor Applet Body Cookies Form Image Link Title URL , etc JavaScript Introduction

Document Object DOM methods- Open() Close() getElementByName() getElementBYId() Write() Writeln() JavaScript Introduction

Validation in JavaScript Form Validation- Form data that typically are checked by a JavaScript could be: has the user left required fields empty? has the user entered a valid e-mail address? has the user entered a valid date? has the user entered text in a numeric field? Ex: Blank/ or null validation var x= document.forms["myForm"]["fname"].value if (x==null || x=="") // for Null or blank value Email validation var x=document.forms["myForm"]["email"].value; var atpos=x.indexOf("@"); var dotpos=x.lastIndexOf("."); if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) JavaScript Introduction