Technical University of Sofia Faculty of Computer Systems and Control. Web Programming. Lecture 4 JavaScript

Similar documents
«W3Schools Home Next Chapter» JavaScript is THE scripting language of the Web.

JavaScript: Introduction to Scripting Pearson Education, Inc. All rights reserved.

JavaScript Basics & HTML DOM. Sang Shin Java Technology Architect Sun Microsystems, Inc. sang.shin@sun.com

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

JavaScript Introduction

JavaScript and Dreamweaver Examples

PHP Tutorial From beginner to master

LAB MANUAL CS (22): Web Technology

Example. Represent this as XML

MASTERTAG DEVELOPER GUIDE

Government Girls Polytechnic, Bilaspur

JavaScript: Control Statements I

Web Programming Step by Step

c. Write a JavaScript statement to print out as an alert box the value of the third Radio button (whether or not selected) in the second form.

IMRG Peermap API Documentation V 5.0

Complete this module and Complete the JavaScript module at

Implementing Specialized Data Capture Applications with InVision Development Tools (Part 2)

ANGULAR JS SOFTWARE ENGINEERING FOR WEB SERVICES HASAN FAIZAL K APRIL,2014

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

JavaScript: Arrays Pearson Education, Inc. All rights reserved.

Short notes on webpage programming languages

Finding XSS in Real World

JavaScript. JavaScript: fundamentals, concepts, object model. Document Object Model. The Web Page. The window object (1/2) The document object

Intell-a-Keeper Reporting System Technical Programming Guide. Tracking your Bookings without going Nuts!

FAQ: troubleshooting Java for Saba Web Access

Client-side Web Engineering From HTML to AJAX

Web Development CSE2WD Final Examination June (a) Which organisation is primarily responsible for HTML, CSS and DOM standards?

Overview. In the beginning. Issues with Client Side Scripting What is JavaScript? Syntax and the Document Object Model Moving forward with JavaScript

Introduction to Java

JAVASCRIPT AND COOKIES

Web Development and Core Java Lab Manual V th Semester

Yandex.Widgets Quick start

Lecture 2 Notes: Flow of Control

Website Login Integration

Fortigate SSL VPN 3.x With PINsafe Installation Notes

Slide.Show Quick Start Guide

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Website Implementation

The purpose of jquery is to make it much easier to use JavaScript on your website.

DC60 JAVA AND WEB PROGRAMMING JUNE b. Explain the meaning of the following statement public static void main (string args [ ] )

Dynamic Web-Enabled Data Collection

J a v a Quiz (Unit 3, Test 0 Practice)

INFORMATION BROCHURE Certificate Course in Web Design Using PHP/MySQL

Client-side programming with JavaScript. Laura Farinetti Dipartimento di Automatica e Informatica Politecnico di Torino laura.farinetti@polito.

Accessing the Online Meeting Room (Blackboard Collaborate)

WIRIS quizzes web services Getting started with PHP and Java

Fortigate SSL VPN 4 With PINsafe Installation Notes

Internet Technologies

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Understanding Cross Site Scripting

Portals and Hosted Files

Lab 5 Introduction to Java Scripts

SUBJECT CODE : 4074 PERIODS/WEEK : 4 PERIODS/ SEMESTER : 72 CREDIT : 4 TIME SCHEDULE UNIT TOPIC PERIODS 1. INTERNET FUNDAMENTALS & HTML Test 1

Online Giving User Guide for Church Members

A Tale of the Weaknesses of Current Client-Side XSS Filtering

Data Transfer Tips and Techniques

If you see "Skip installation of the current version and test the currently installed version of Java" then select that hyperlink.

HTML Basics(w3schools.com, 2013)

This matches a date in the MM/DD/YYYY format in the years The date must include leading zeros.

PLAYER DEVELOPER GUIDE

ICT 6012: Web Programming

Usage Tracking for IBM InfoSphere Business Glossary

Debugging JavaScript and CSS Using Firebug. Harman Goei CSCI 571 1/27/13

Technical University of Sofia Faculty of Computer Systems and Control. Web Programming. Lecture 5 JavaScript part 2

Setup The package simply needs to be installed and configured for the desired CDN s distribution server.

Web Application Development

Example of a Java program

Configuring iplanet 6.0 Web Server For SSL and non-ssl Redirect

VB.NET Programming Fundamentals

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Reference Guide for WebCDM Application 2013 CEICData. All rights reserved.

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Edexcel Online FS ICT On Demand Download of Papers

Hello World RESTful web service tutorial

Angel Learning Management System Introductory Tutorial for Students

C O N F I G U R I N G Y O U R W E B B R O W S E R TO A L L O W P O P - U P W I N D O W S

Integrated Pictometry Analytics Quick Start Guide. July 2012

COURSE SYLLABUS EDG 6931: Designing Integrated Media Environments 2 Educational Technology Program University of Florida

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Technical Guide for Remote access

Secure Mail Registration and Viewing Procedures

by Jonathan Kohl and Paul Rogers 40 BETTER SOFTWARE APRIL

Further web design: HTML forms

Tutorial 6 Creating a Web Form. HTML and CSS 6 TH EDITION

RSS Feeds - Content Syndication Feed2JS: a simple solution to display RSS feeds

Bitrix Site Manager 4.1. User Guide

1 Introduction. 2 An Interpreter. 2.1 Handling Source Code

Phishing by data URI

Login with Amazon. Getting Started Guide for Websites. Version 1.0

Project 2: Web Security Pitfalls

CIS 445 Advanced Visual Basic.NET ASP.NET

Transcription:

Technical University of Sofia Faculty of Computer Systems and Control Web Programming Lecture 4 JavaScript

JavaScript basics JavaScript is scripting language for Web. JavaScript is used in billions of Web pages to add functionality, validate forms, communicate with the server, and much more. JavaScript is easy to learn. You will enjoy it. Works in all major browsers, such as Internet Explorer, Firefox, Chrome, Opera, and Safari. 11/13/2014 Assist. Prof. Antonia Tasheva 2

Example <html> <head> <script type="text/javascript"> function displaydate() { document.getelementbyid("demo").innerhtml=date(); } </script> </head> <body> <h3>my First JavaScript Code</h3> <p id="demo">this is a paragraph.</p> <button type="button" onclick="displaydate()"> Display Date </button> </body> </html> 11/13/2014 Assist. Prof. Antonia Tasheva 3

What is JavaScript? A scripting language is a lightweight programming language JavaScript is an interpreted language (means that scripts execute without preliminary compilation) JavaScript was designed to add interactivity to HTML pages and is usually embedded directly into them Everyone can use JavaScript without purchasing a license 11/13/2014 Assist. Prof. Antonia Tasheva 4

Java and JavaScript Java and JavaScript are two completely different languages in both concept and design! Java is developed by Sun Microsystems. Java is a powerful and much more complex programming language - in the same category as C and C++. 11/13/2014 Assist. Prof. Antonia Tasheva 5

What Can JavaScript do? JavaScript gives HTML designers a programming tool - HTML authors are normally not programmers, but JavaScript is a scripting language with a very simple syntax! Almost anyone can put small "snippets" of code into their HTML pages. 11/13/2014 Assist. Prof. Antonia Tasheva 6

What Can JavaScript do? JavaScript can react to events - A JavaScript can be set to execute when something happens, like when a page has finished loading or when a user clicks on an HTML element 11/13/2014 Assist. Prof. Antonia Tasheva 7

What Can JavaScript do? JavaScript can read and write HTML elements - A JavaScript can read and change the content of an HTML element. JavaScript can be used to validate data - A JavaScript can be used to validate form data before it is submitted to a server. This saves the server from extra processing 11/13/2014 Assist. Prof. Antonia Tasheva 8

What Can JavaScript do? JavaScript can be used to detect the visitor's browser - A JavaScript can be used to detect the visitor's browser, and - depending on the browser - load another page specifically designed for that browser. JavaScript can be used to create cookies - A JavaScript can be used to store and retrieve information on the visitor's computer 11/13/2014 Assist. Prof. Antonia Tasheva 9

How to add JS? JavaScripts can be put in the <body> and in the <head> sections of an HTML page. Note that the JavaScript should be placed at the bottom of the page to make sure it is not executed before the elements manipulated in it are created. 11/13/2014 Assist. Prof. Antonia Tasheva 10

When it executes? JavaScripts in an HTML page will be executed when the page loads. This is not always what we want. Sometimes we want to execute a JavaScript when an event occurs, such as when a user clicks a button. When this is the case we can put the script inside a function. 11/13/2014 Assist. Prof. Antonia Tasheva 11

Scripts in <head> and <body> You can place an unlimited number of scripts in your document, and you can have scripts in both the body and the head section at the same time. It is a common practice to put all functions in the head section, or at the bottom of the page. This way they are all in one place and do not interfere with page content. 11/13/2014 Assist. Prof. Antonia Tasheva 12

External JavaScript JavaScript can also be placed in external files. External JavaScript files often contain code to be used on several different web pages. External JavaScript files have the file extension.js External script cannot contain the <script></script> tags! 11/13/2014 Assist. Prof. Antonia Tasheva 13

Import external JavaScript To use an external script, point to the.js file in the "src" attribute of the <script> tag: <html> <head> <script type="text/javascript src= name.js"></script> </head> <body> </body> </html> 11/13/2014 Assist. Prof. Antonia Tasheva 14

JavaScript Statements JavaScript is a sequence of statements to be executed by the browser. A JavaScript statement is a command to a browser. The purpose of the command is to tell the browser what to do. JavaScript is case sensitive - therefore watch your capitalization closely when you write JavaScript statements, create or call variables, objects and functions. 11/13/2014 Assist. Prof. Antonia Tasheva 15

JavaScript Code JavaScript code (or just JavaScript) is a sequence of JavaScript statements. Each statement is executed by the browser in the sequence they are written. This example will write a heading and two paragraphs to a web page: 11/13/2014 Assist. Prof. Antonia Tasheva 16

JavaScript Code - Example This example will write a heading and two paragraphs to a web page: <html> <body> Text in Body of page. <script type="text/javascript"> document.write("<h1>this is a heading</h1>"); document.write("<p>this is a paragraph.</p>"); document.write("<p>this is another paragraph.</p>"); </script> Other text. </body> </html> 11/13/2014 Assist. Prof. Antonia Tasheva 17

Examples Example 1 Example 2 Example 3 11/13/2014 Assist. Prof. Antonia Tasheva 18

JavaScript Variables Variables are used to store data. A variable is a "container" for information you want to store. A variable's value can change during the script. You can refer to a variable by name to see its value or to change its value. Rules for variable names: Variable names are case sensitive They must begin with a letter or the underscore character strname STRNAME (not same) 11/13/2014 Assist. Prof. Antonia Tasheva 19

JavaScript Variables - Types There is Dynamic Typing variables can hold any valid type of value: Number var myint = 7; Boolean var mybool = true; Function (for later) Object (for later) Array var myarr = new Array(); String var mystring = abc ; and can hold values of different types at different times during execution. 11/13/2014 Assist. Prof. Antonia Tasheva 20

JavaScript Operators Arithmetic Operators Operator Description Example Result + Addition x=2 4 y=2 x+y - Subtraction x=5 3 y=2 x-y * Multiplication x=5 20 y=4 x*y / Division 15/5 3 % Modulus (division remainder) 5/2 2,5 5%2 1 10%8 2 10%2 0 ++ Increment x=5 x=6 x++ -- Decrement x=5 x=4 x-- 11/13/2014 Assist. Prof. Antonia Tasheva 21

JavaScript Operators Assignment Operators Operator Example Is The Same As = x=y x=y += x+=y x=x+y -= x-=y x=x-y *= x*=y x=x*y /= x/=y x=x/y %= x%=y x=x%y 11/13/2014 Assist. Prof. Antonia Tasheva 22

JavaScript Operators Comparison Operators Operator Description Example == is equal to 5==8 returns false === is equal to (checks for both value and type) x=5 y="5" x==y returns true x===y returns false!= is not equal 5!=8 returns true > is greater than 5>8 returns false < is less than 5<8 returns true >= is greater than or equal to 5>=8 returns false <= is less than or equal to 5<=8 returns true 11/13/2014 Assist. Prof. Antonia Tasheva 23

JavaScript Operators Logical Operators Operator Description Example && and x=6 y=3 or x=6 (x < 10 && y > 1) returns true y=3! not x=6 (x==5 y==5) returns false y=3!(x==y) returns true 11/13/2014 Assist. Prof. Antonia Tasheva 24

Example <html> <body> <script> x="hello World!" document.write(x) document.write("<br>") </script> <script> x="good BYE" document.write("class" +x) </script> </body> </html> 11/13/2014 Assist. Prof. Antonia Tasheva 25

JavaScript Popup Boxes Alert Box An alert box is often used if you want to make sure information comes through to the user. When an alert box pops up, the user will have to click "OK" to proceed. <script> alert("hello World!") </script> 11/13/2014 Assist. Prof. Antonia Tasheva 26

JavaScript Popup Boxes Confirm Box A confirm box is often used if you want the user to verify or accept something. When a confirm box pops up, the user will have to click either "OK" or "Cancel" to proceed. If the user clicks "OK", the box returns true. If the user clicks "Cancel", the box returns false. 11/13/2014 Assist. Prof. Antonia Tasheva 27

JavaScript Popup Boxes Prompt Box A prompt box is often used if you want the user to input a value before entering a page. When a prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed after entering an input value. If the user clicks "OK, the box returns the input value. If the user clicks "Cancel, the box returns null. 11/13/2014 Assist. Prof. Antonia Tasheva 28

Prompt Box Example <script> x=prompt ( WELCOME TO, ) document.write( CLASS <br>,+x) </script> 11/13/2014 Assist. Prof. Antonia Tasheva 29

JS Examples Y=20x+12 ;where x=3 <script> x=3; y=20*x+12; alert(y); </script> The answer should be? 11/13/2014 Assist. Prof. Antonia Tasheva 30

JS Examples <script> s1=12; s2=28; sum=s1+s2; document.write( Sum of numbers is: "+sum); </script> 11/13/2014 Assist. Prof. Antonia Tasheva 31

JS Examples <script> s1=12; s2=6; add=s1+s2; sub=s1-s2; mul=s1*s2; divl=s1/s2; document.write(add+"<br>"); document.write(sub+"<br>"); document.write(mul+"<br>"); document.write(divl+"<br>"); alert("caculation COMPLETED"); </script > 11/13/2014 Assist. Prof. Antonia Tasheva 32

Conditional Statements Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this. In JavaScript we have the following conditional statements: if statement - use this statement if you want to execute some code only if a specified condition is true if...else statement - use this statement if you want to execute some code if the condition is true and another code if the condition is false if...else if...else statement - use this statement if you want to select one of many blocks of code to be executed switch statement - use this statement if you want to select one of many blocks of code to be executed 11/13/2014 Assist. Prof. Antonia Tasheva 33

Conditional Statements if (condition) { code to be executed if condition is true } ------------------------------------------------------------------------ if (condition) { code to be executed if condition is true } else { code to be executed if condition is not true } 11/13/2014 Assist. Prof. Antonia Tasheva 34

Conditional Statements Examples <script> x=3; if(x<0) { alert ("negative"); } else { alert ("pozitive"); } </script> 11/13/2014 Assist. Prof. Antonia Tasheva 35

Conditional Statements Examples <script> c=confirm( IS 25 divisible by 5? ); if(c) { alert ( Correct answer ); } else { alert ( wrong answer ); } </script> 11/13/2014 Assist. Prof. Antonia Tasheva 36

Conditional Statements Examples <script> p=prompt( ENTER THE MARK? (2 to 6)", " "); if(p>= 3") { alert( PASS"); } else { alert( FAIL"); } </script> 11/13/2014 Assist. Prof. Antonia Tasheva 37

Conditional Statements switch switch (myvar) { case 1: // if myvar is equal to 1 this is executed case two : // if myvar is equal to two this is executed case default: // if none of the cases above are satisfied OR if // no break statement is used in the cases // above, this will be executed } 11/13/2014 Assist. Prof. Antonia Tasheva 38

JS Functions Declaring a function by using the function keyword No return type, nor typing of arguments function add (numone, numtwo) { return numone + numtwo; } Calling a function myfunc (arg1, arg2, ); add (4,5); 11/13/2014 Assist. Prof. Antonia Tasheva 39

JS Functions Example <html> <head> <script Language="JavaScript"> function getmax(n1, n2){ if (n1 < n2) return n2; else return n1; } </script> </head> <body> <script Language="JavaScript"> document.write("<p>") ; document.write( Largest number of 5 or 6 is: " + getmax(5, 6)); document.write("</p>") </script> </body> </html> 11/13/2014 Assist. Prof. Antonia Tasheva 40

Thank you for your attention! 11/13/2014 Assist. Prof. Antonia Tasheva 41