JavaScript s role on the Web

Similar documents
Introduction to JavaScript

<form>. input elements. </form>

What is Java Script? Writing to The HTML Document. What Can JavaScript do? CMPT 165: Java Script

Q1. What is JavaScript?

CGS 3066: Spring 2015 JavaScript Reference

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

JavaScript Introduction

CSC Web Programming. Introduction to JavaScript

Learning JavaScript. A C P K Siriwardhana, BSc, MSc

Session 16. JavaScript Part 1. Reading

Session 6. JavaScript Part 1. Reading

CHAPTER 6 JAVASCRIPT PART 1

CISC 1600 Lecture 2.4 Introduction to JavaScript

Lecture 14. Introduction to JavaScript. Mr. Mubashir Ali Lecturer (Dept. of Computer Science)

INTRODUCTION TO WEB DEVELOPMENT AND HTML. Lecture 13: Intro to JavaScript - Spring 2011

JavaScript!= Java. Intro to JavaScript. What is JavaScript? Intro to JavaScript 4/17/2013 INTRODUCTION TO WEB DEVELOPMENT AND HTML

Javascript. UNIVERSITY OF MASSACHUSETTS AMHERST CMPSCI 120 Fall 2010

Princess Nourah bint Abdulrahman University. Computer Sciences Department

By the end of this section of the practical, the students should be able to:

5. JavaScript Basics

Last &me: Javascript (forms and func&ons)

JavaScript code is inserted between tags, just like normal HTML tags:

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

CSS The web browser uses its own resources, and eases the burden on the server. It has fewer features than server side scripting.

Web Development & Design Foundations with HTML5

JavaScript Introduction

Then there are methods ; each method describes an action that can be done to (or with) the object.

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

JavaScript CSCI 201 Principles of Software Development

By the end of this section of the practical, the students should be able to:

Just add an HTML comment tag <!-- before the first JavaScript statement, and an end-of comment tag --> after the last JavaScript statement, like this:

Working with JavaScript

The first sample. What is JavaScript?

Web Programming/Scripting: JavaScript

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

Web Programming and Design. MPT Junior Cycle Tutor: Tamara Demonstrators: Aaron, Marion, Hugh

8/19/2018. Web Development & Design Foundations with HTML5. Learning Objectives (1 of 2) Learning Objectives (2 of 2) What is JavaScript?

<script type="text/javascript"> script commands </script>

Let's Look Back. We talked about how to create a form in HTML. Forms are one way to interact with users

CITS1231 Web Technologies. JavaScript

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

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

New Media Production Lecture 7 Javascript

JavaScript CS 4640 Programming Languages for Web Applications

Exercise 1: Basic HTML and JavaScript

Introduction to using HTML to design webpages

LECTURE-3. Exceptions JS Events. CS3101: Programming Languages: Javascript Ramana Isukapalli

1. Variables, Again 2. JavaScript Operators Types 3. Iteration: the for-loop 4. Working with Strings 5. Making Decisions 6.

CISH-6510 Web Application Design and Development. Overview of JavaScript. Overview

JavaScript is described in detail in many books on the subject, and there is excellent tutorial material at

Web Site Development with HTML/JavaScrip

Fundamentals of Website Development

About the Author. Liability

Software. Programming Languages. Types of Software. Types of Languages. Types of Programming. Software does something

CSC Javascript

JavaScript CS 4640 Programming Languages for Web Applications

Client vs Server Scripting

CSC Software I: Utilities and Internals. Programming in Python

The Web: Concepts and Technology

B. V. Patel Institute of BMC & IT 2014

1. Cascading Style Sheet and JavaScript

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

Javascript Lecture 23

1$ 5 ! $ 6 4 * Source: 0 "!*! 0! * 8 97 ?!$ 5 0 *! 4! $ 0 : * ' () 7;7 7<7

COMS 469: Interactive Media II

Introduction to DHTML

Internet Technologies 5-Dynamic Web. F. Ricci 2010/2011

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

JavaScript Programming

JAVASCRIPT. sarojpandey.com.np/iroz. JavaScript

Notes General. IS 651: Distributed Systems 1

New Perspectives on Creating Web Pages with HTML. Tutorial Objectives

JavaScript Introduction

SEEM4570 System Design and Implementation Lecture 03 JavaScript

What is PHP? [1] Figure 1 [1]

Chapter 16. JavaScript 3: Functions Table of Contents

Produced by. App Development & Modeling. BSc in Applied Computing. Eamonn de Leastar

Week 13 Thursday (with Page 5 corrections)

Chapter4: HTML Table and Script page, HTML5 new forms. Asst. Prof. Dr. Supakit Nootyaskool Information Technology, KMITL

A Balanced Introduction to Computer Science, 3/E David Reed, Creighton University 2011 Pearson Prentice Hall ISBN

Consider the following file A_Q1.html.

COMS W3101: SCRIPTING LANGUAGES: JAVASCRIPT (FALL 2017)

Client-Side Web Technologies. JavaScript Part I

JavaScript. Asst. Prof. Dr. Kanda Saikaew Dept. of Computer Engineering Khon Kaen University

Functions. INFO/CSE 100, Spring 2006 Fluency in Information Technology.

Syllabus - July to Sept

Program Design Phase. Algorithm Design - Mathematical. Algorithm Design - Sequence. Verify Algorithm Y = MX + B

Manual Html A Href Onclick Submit Form

Programming Basics. INFO/CSE 100, Spring 2006 Fluency in Information Technology.

Document Object Model (DOM)

JAVASCRIPT HTML DOM. The HTML DOM Tree of Objects. JavaScript HTML DOM 1/14

Javascript. UNIVERSITY OF MASSACHUSETTS AMHERST CMPSCI 120 Fall 2010

JavaScript by Vetri. Creating a Programmable Web Page

JavaScript Specialist v2.0 Exam 1D0-735

Enhancing Web Pages with JavaScript

Forms, Form Events, and Validation. Bok, Jong Soon

The PHP language. Teaching you everything about PHP? Not exactly Goal: teach you how to interact with a database via web

Assessment - Unit 3 lessons 16-21

Lesson 3: Basic Programming Concepts

People = End Users & Programmers. The Web Browser Application. Program Design Phase. Algorithm Design -Mathematical Y = MX + B

Transcription:

Chris Panayiotou

JavaScript s role on the Web JavaScript Programming Language Developed by Netscape for use in Navigator Web Browsers Purpose make web pages (documents) more dynamic and interactive Change contents of document, provide forms and controls, animation, control web browser window, etc.

JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language i.e. lightweight programming language A JavaScript is usually embedded directly in HTML pages JavaScript is an interpreted language (means that scripts execute without preliminary compilation) Converts code to executable format each time it runs Converted when browser loads a web document JavaScript is supported by all major browsers Java and Javascript are not the same although they share some common syntax

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 Puts dynamic text into an HTML page A JavaScript statement like this: document.write("<h1>" + name + "</h1>") can write a variable text into an HTML page 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 Can read and write HTML elements Can be used to validate data (e.g. form data) before it is submitted to a server

Typical programming elements: variables, operators, expressions, statements, definition of functions Connection to Web built-in functions (e.g., Date()) Document Object Model = names of things, properties, methods (e.g., document.write) events (e.g., onsubmit, onmouseover)

Inserting JavaScript into an HTML page <html> <body> <script type="text/javascript"> document.write("hello World!") </script> </body> </html> Note that semicolons are optional

JavaScript is usually placed in the HEAD section Make sure that the function is loaded before used <html> <head> <script type="text/javascript"> some statements </script> </head> <body> Some HTML </body> You can place any number of scripts in one page Can write code to a file and load the file <html> <head> <script src="xxx.js"></script> </head> <body> </body> </html>

Used to store information Variable name are case sensitive Must begin with a letter or the underscore Example: var strname = Chris Or strname = Chris The scope of variables is as usual Local within the functions Global (page level) if declared outside the functions

Operator Description Example Result + Addition - Subtraction * Multiplication / Division % Modulus (division remainder) ++ Increment -- Decrement x=2 x+2 x=2 5-x x=4 x*5 Arithmetic operators 4 3 20 15/5 3 5/2 2.5 5%2 1 10%8 2 10%2 0 x=5 x++ x=5 x-- x=6 x=4 Operator Example = x=y x=y Is The Same As += 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 Assignment Operators

Operator Description Example == is equal to!= is not equal > is greater than < is less than >= <= is greater than or equal to Comparison is less than or equal to 5==8 returns false 5!=8 returns true 5>8 returns false 5<8 returns true 5>=8 returns false 5<=8 returns true Operator Description Example && Logical and or! not x=6, y=3 (x < 10 && y > 1) returns true x=6, y=3 (x==5 y==5) returns false x=6, y=3!(x==y) returns true

Writing formatted text <html> <body> <script type="text/javascript"> document.write("<h1>hello World!</h1>") </script> </body> </html>

As in Java Example: txt1= What a very txt2= nice day! txt3=txt1 + + txt2 txt3 contains What a very nice day! Creating functions function myfunction(argument1,argument2,etc) { some statements Functions with returning values function result(a,b) { c=a+b return c Calling Functions myfunction(argument1,argument2,etc) Concatenation Functions

<script type="text/javascript"> // If the time on your browser // is less than 12, you will get // a Good morning greeting. var d=new Date() var time=d.gethours() if (time<10) { document.write("<b>good morning</b>") else { document.write("<b>good afternoon</b>") </script> If and If..Else statement while while (condition) { code to be executed do...while do { code to be executed while (condition) for for (initial; condition; increment) { code to be executed Loops

Switch <script type="text/javascript"> //You will receive a different greeting based on what day it is. Note that Sunday=0, //Monday=1, Tuesday=2, etc. var d=new Date() theday=d.getday() switch (theday) { case 5: document.write("finally Friday") break case 6: document.write("super Saturday") break case 0: document.write("sleepy Sunday") break default: document.write("i'm looking forward to this weekend!") </script>

<html> <head> <title> JS1 </title> </head> <body> <a href="http://www.google.com " onmouseover="status= Happy Googling'; return true; > Google search </a> </body> </html> This is an event. This lets the hyperlink take place. Note single quotes inside double quotes

var input = prompt( What is your name?, Chris ); print("well hello " + (input dear )); If the user presses 'Cancel' or closes the prompt dialog in some other way without giving a name, the variable input will hold the value null or Both of these would give false when converted to a boolean The expression input "dear" can in this case be read as the value of the variable input, or else the string dear It is an easy way to provide a fallback value.

Similar to Java try { catch (error) { finally { statements function lastelement(array) { if (array.length > 0) return array[array.length 1] else throw Can not take the last element of an empty array. function lastelementplusten(array) { return lastelement(array) + 10 try { print(lastelementplusten([])); catch (error) { print( Something went wrong:, error) finally { print( Finished")

Document Object Model (DOM) Contains all HTML elements loaded in a web page Can be used by scripting languages to manipulate HTML on the fly DOM objects: Anchor (hyperlink) Body Button Checkbox Document Form Frame Frameset Image Link Location Password Radio Select Style Submit Table TableData TableHeader TableRow Text Textarea Window

<html> <head> <script type="text/javascript"> function myhref() { document.getelementbyid('myanchor').innertext="visit W3Schools" document.getelementbyid('myanchor').href="http://www.w3schools.com" </script> </head> <body> <a id="myanchor" href="http://www.microsoft.com">visit Microsoft</a> <form> <input type="button" onclick="myhref()" value="change URL and text"> </form> </body> </html>

<html> <head> <script type="text/javascript"> function showformelements(theform) { str="form Elements: " for (i=0; i<theform.length; i++) str+= \n +theform.elements[i].name alert(str) </script> </head> <body> <form> First name: <input type="text" name="fname" size="20"> <br /> Last name: <input type="text" name="lname" size="20"> <br /><br /> <input type= button name= button1 value= Display name of form elements onclick= showformelements(this.form) > </form> </body> </html>

<html> <head> <script type="text/javascript"> function formsubmit() { document.forms.myform.submit() </script> </head> <body> <form name="myform" action= sample2.asp" method="get"> Firstname: <input type="text" name="firstname" size="20"><br/> Lastname: <input type="text" name="lastname" size="20"><br/><br/> <input type="button" onclick="formsubmit()" value="submit"> </form> </body> </html>

<html> <head> <script type="text/javascript"> function validate() { x=document.myform at=x.email.value.indexof("@") if (at == -1) { alert("not a valid e-mail") return false </script> </head> <body> <form name="myform" action="tryjs_submitpage.htm" onsubmit="return validate()"> Enter your E-mail: <input type="text" name="email" size="20"> <input type="submit" value="submit"> </form> <p><b>note:</b> This example ONLY tests if the e-mail address contains an "@" character. A "reallife" code would have to test for punctuations, spaces and other things as well.</p> </body> </html>

http://www.javascript.com http://www.pageresource.com/jscript/ http://www.htmlgoodies.com/beyond/javascr ipt/index.php http://javascript.internet.com/ http://javascriptkit.com/ http://www.w3schools.com/js/default.asp Use Google

Create a page with JavaScript to do the following. These can all be on one page. Prompt the user for their name. Use a pop-up box to welcome the user by name. Display the current date on the page in the following format: October 30, 2000. Do not display the time. Do not "hard code" the date; if I load the page tomorrow, I should get a different date than if I load it today. Display the last modified date of the document. Put some useful advice, on any subject, in the status line of the browser. Display a message saying Good Morning if it is in the morning, Good Afternoon if it is in the afternoon, and Good Evening if it is in the Evening.

JavaScript Form Validation Exercise Create a form that includes 2 textboxes (one for name, one for email address), a textarea, a dropdown box (<select> tag), and a set of checkboxes. Format the form using a table. The ACTION attribute of the form tag should be to email the form results to your email address. Feel free to use a preexisting form rather than creating one for this exercise. Add JavaScript event handlers to the form to validate the form before it is emailed to you. If there is a problem, display the problem with the page in some way. Possible ways to do this are: a pop-up box displaying the problems in a new window in red text and having the user use the back button to go back to the original page displaying the problems in the current window at the top of the page in red text Validate that an entry has been made in each form element Validate that the email address contains an @ sign. If you have time and want to try something more, add a second drop down list and make this second one dynamic, i.e. make the choices that appear in the second drop down list depend on what choice is made in the first list.