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

Similar documents
Place User-Defined Functions in the HEAD Section

JavaScript Handling Events Page 1

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

Session 16. JavaScript Part 1. Reading

JavaScript and XHTML. Prof. D. Krupesha, PESIT, Bangalore

A Balanced Introduction to Computer Science, 3/E

Session 6. JavaScript Part 1. Reading

Introduction to using HTML to design webpages

CISC 1600 Lecture 2.4 Introduction to JavaScript

Web Programming/Scripting: JavaScript

Events: another simple example

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

Introduction to JavaScript

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

New Perspectives on Creating Web Pages with HTML. Tutorial Objectives

Q1. What is JavaScript?

Pearson Education Limited Edinburgh Gate Harlow Essex CM20 2JE England and Associated Companies throughout the world

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

CGS 3066: Spring 2015 JavaScript Reference

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

LECTURE-2. Functions review HTML Forms. Arrays Exceptions Events. CS3101: Scripting Languages: Javascript Ramana Isukapalli

Want to add cool effects like rollovers and pop-up windows?

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

JavaScript CS 4640 Programming Languages for Web Applications

Fundamentals of Website Development

JavaScript CS 4640 Programming Languages for Web Applications

Lesson 5: Introduction to Events

Introduction to DHTML

CSC Web Programming. Introduction to JavaScript

The first sample. What is JavaScript?

COMS W3101: SCRIPTING LANGUAGES: JAVASCRIPT (FALL 2017)

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

<form>. input elements. </form>

JAVASCRIPT BASICS. Handling Events In JavaScript. In programing, event-driven programming could be a programming

Javascript. A key was pressed OR released. A key was released. A mouse button was pressed.

Key features. Nothing to do with java It is the Client-side scripting language Designed to add interactivity to HTML pages

VectorDraw web Library

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

CSCU9B2 Practical 1: Introduction to HTML 5

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

IS 242 Web Application Development I

INLEDANDE WEBBPROGRAMMERING MED JAVASCRIPT INTRODUCTION TO WEB PROGRAMING USING JAVASCRIPT

Photo from DOM

CSC Javascript

Such JavaScript Very Wow

Web Security, Summer Term 2012

Web Security, Summer Term 2012

extc Web Developer Rapid Web Application Development and Ajax Framework Using Ajax

Mouse-over Effects. Text based buttons

JS Tutorial 3: InnerHTML Note: this part is in last week s tutorial as well, but will be included in this week s lab

Web Development & Design Foundations with HTML5, 8 th Edition Instructor Materials Chapter 14 Test Bank

JavaScript s role on the Web

Javascript. UNIVERSITY OF MASSACHUSETTS AMHERST CMPSCI 120 Fall 2010

Module 5 JavaScript, AJAX, and jquery. Module 5. Module 5 Contains 2 components

Javascript Lecture 23

Ajax- XMLHttpResponse. Returns a value such as ArrayBuffer, Blob, Document, JavaScript object, or a DOMString, based on the value of

The course also includes an overview of some of the most popular frameworks that you will most likely encounter in your real work environments.

HTML 5 and CSS 3, Illustrated Complete. Unit L: Programming Web Pages with JavaScript

JavaScript II CSCI 311 SPRING 2017

Basic Uses of JavaScript: Modifying Existing Scripts

Command-driven, event-driven, and web-based software

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

Client vs Server Scripting

Lecture 5. Connecting Code to Web Page Events

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

DOM. Ajax Technology in Web Programming. Sergio Luján Mora. DLSI - Universidad de Alicante 1. API for accessing and manipulating HTML documents

Programing for Digital Media EE1707. Lecture 4 JavaScript By: A. Mousavi & P. Broomhead SERG, School of Engineering Design, Brunel University, UK

ORB Education Quality Teaching Resources

Outline. Lecture 4: Document Object Model (DOM) What is DOM Traversal and Modification Events and Event Handling

link document.getelementbyid("coffee").style.borderwidth = "0px" document.getelementbyid("tea").style.borderwidth = "10px"

For loops and Window Objects:

How to set up a local root folder and site structure

CIW 1D CIW JavaScript Specialist.

UNIT 3 SECTION 1 Answer the following questions Q.1: What is an editor? editor editor Q.2: What do you understand by a web browser?

4. Structure of a C++ program

Dynamic documents with JavaScript

Web Development & Design Foundations with HTML5

JavaScript by Vetri. Creating a Programmable Web Page

COMSC-030 Web Site Development- Part 1. Part-Time Instructor: Joenil Mistal

HTML5 - INTERVIEW QUESTIONS

New Media Production Lecture 7 Javascript

Higher Computing Science

INTRODUCTION TO JAVASCRIPT

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

Dynamic Web Pages - Integrating JavaScript into a SAS Web Application Caroline Bahler, ASG, Inc.

Web Site Design and Development JavaScript

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

HTML User Interface Controls. Interactive HTML user interfaces. Document Object Model (DOM)

Review of HTML. Chapter Pearson. Fundamentals of Web Development. Randy Connolly and Ricardo Hoar

CSE 115. Introduction to Computer Science I

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

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

EVENT-DRIVEN PROGRAMMING

Varargs Training & Software Development Centre Private Limited, Module: HTML5, CSS3 & JavaScript

GRITS AJAX & GWT. Trey Roby. GRITS 5/14/09 Roby - 1

Programming Lab 1 (JS Hwk 3) Due Thursday, April 28

Project 3 Web Security Part 1. Outline

EXERCISE: Introduction to client side JavaScript

Tutorial 10: Programming with JavaScript

Transcription:

JavaScript (last updated April 15, 2013: LSS) JavaScript is a scripting language, specifically for use on web pages. It runs within the browser (that is to say, it is a client- side scripting language), and it can be used to make web pages dynamic, that is, able to respond to events (such as mouse movements) and able to allow users to fill in forms which can then be sent to the server. In fact, JavaScript can do a great deal more than this. JavaScript is described in detail in many books on the subject, and there is excellent tutorial material at http://www.w3schools.com/js/default.asp. Basics JavaScript is embedded in the HTML sent from the server to the browser. It is placed inside a <script> </script> tag: <script> document.write("hello World!"); </script> (In earlier versions of HTML, the attribute type="text/javascript" was required inside the <script> tag, but this is no longer the case for HTML5.) 1 The script may be placed either in the header or the body of the HTML: if placed in the body, the JavaScript will be executed when the browser reaches that part of the HTML file. Those parts placed in the header will not be executed until (or unless) called (see later). It is also possible to place (some or all of the) the JavaScript in an external file using the attribute src, enabling sharing of JavaScript across web pages. For example, <script src= scripts/usethisscript.js > </script> would use the JavaScript file usethisscript.js located in the local folder scripts. Note that the JavaScript file may be on a completely different machine: the source (src) may be a full URL. 1 Also, in the past the JavaScript was often placed inside HTML comment brackets <!-- -->, so that if the browser did not support JavaScript, the JavaScript would have no effect. But these days, virtually all browsers do support JavaScript. LSS 2013 1

JavaScript is object oriented, which is to say, it sees the page it is inside, as an object with many sub- objects. The name for this structure is the Document Object Model (DOM), and for each web page this takes the form of a tree, with the document at the root. Figure 1: The Domain Object Model for a simple web page consisting of a head, with a title, and a body with a link and a header. In addition, JavaScript has access to the identity of the browser itself (and, indeed, to the browser history), as well as to the display, and to the date and time. JavaScript can create new browser windows, as well as manipulating the contents of the window it is in. JavaScript sees all of these entities (browser, window, date etc.) as objects, and can access their constituent parts as object properties and/or use object methods to manipulate them. How to use all of these facilities is well beyond the scope of this document, however: there are quite a number of books available. The navigator object This is one of the simplest objects. It refers to the browser in use, and has just six properties, namely navigator.appcodename which returns the code name of the browser, navigator.appname which returns the name of the browser, navigator.appversion which returns the version information of the browser, navigator.cookieenabled which determines whether cookies are enabled in the browser, navigator.platform which returns for which platform the browser is compiled, and navigator.useragent which returns the user- agent header sent by the browser to the server, LSS 2013 2

and one method, navigator.javaenabled() which returns whether or not the browser has Java enabled What can one use this for? Platforms vary in size from iphone sized, to Galaxy note sized, to ipad sized, to laptop to large screen desktops. Different browsers (Safari, Internet Explorer, Chrome, etc.) have slightly different characteristics, and with a single browser type, different versions have slightly different capabilities. So, for example one might have code like if (navigator.platform = iphone ) // run one set of JavaScript statements else if (navigator.platform = MacIntel // run a different set of JavaScript statements In a similar way, one can test for the browser type and version, and run different JavaScript as required. It is, unfortunately, still the case that browsers are not all compatible, and also that many users are still using older versions of browsers (or browsers that are pretending to be older versions than they actually are!) It would be quite inappropriate to provide the same level of detail on all the other objects but there are books on JavaScript for this purpose. Using JavaScript Although JavaScript may be used in many different ways, the most common way of using it is to place JavaScript functions in the header of the HTML file call these functions (perhaps with parameters) from the body of the HTML file The JavaScript functions may be called from anywhere in the HTML file (JavaScript scripts are executed as they are read by the Browser, so functions may be executed from inside a script placed at any point). However, one of the commonest JavaScript idioms is to call a function in response to an event. There are many different types of events, associated with different tags. For example <body onload= startmessage() > runs the startmessage() function when the page loads. Most HTML tags have associated event attributes which are used to call JavaScript functions. LSS 2013 3

Mouse events and JavaScript. A very common use of JavaScript is to make the web page responsive to interaction using the mouse. Very nearly all tags have the following mouse event attributes: Attribute Value Description onclick script Script run on a mouse click ondblclick script Script run on a mouse double- click onmousedown script Script run when mouse button is pressed onmousemove script Script run when mouse pointer moves onmouseout script Script run when mouse pointer moves out of an element onmouseover script Script run when mouse pointer moves over an element onmouseup script Script run when mouse button is released By using these, one can make nearly any HTML element be affected by mouse activity. So for example, one can have <img alt="programme" name=prog src="programme_orig.png" onmouseover="prog.src='programme_over.png'" onmouseout="prog.src='programme_orig.png'"> which displays an image called programme_orig.png initially. When the mouse is over the image, the onmouseover event occurs, and the image is altered to 'programme_over.png'. When the mouse moves away from the image, the original file 'programme_orig.png' is used again. This type of code is frequently used to alter button images when the mouse is over them. Note that in the above 1. the JavaScript is actually in the tag itself, rather than being in a function call. 2. We refer to the image as prog.src, rather than using its fully qualified name document.images[ prog ].src Declaring variables in JavaScript JavaScript allows you to declare variables: these must be declared before they are used (i.e. they must have been encountered by the browser interpreting the HTML file prior to them being used: this usually means placing the variable declaration in the <body> of the HTML file, rather than in the <head> ). Variables are not typed (unlike Alice or Java or any of the C- based languages). Thus var xyz ; LSS 2013 4

declares a variable xyz that may later be used. It may be assigned a numeric value (e.g. xyz = 5 ; ) or a string (e.g. xyz = Quasimodo ; ). Often variables are initialised at their declaration: var cartype = Fiat ; The usual operators can be used with these variables: +, -, *, /, % (modulus division), ++ (increment), - - (decrement). Note that + can be used to join strings together. The comparison operators include ==,!=, >, <, >=, <=, and also === which tests for equality of both value and type. Logical operators are && (logical and), (logical or), and! (not). Some common JavaScript usages. Javascript is often used to alter the HTML inside a specific tag. Although it is possible to select the specific version of the tag inside a web page directly using the DOM, it is generally easier to (i) label the tag itself and then (ii) to use the specific identified tag. Labelling the tag is done using the id= attribute (and this is available for virtually all HTML5 tags): <p id= paragraph9 >Some text</p> then one can find the object with this identifier (inside a <script> tag): var therightparagraph = document.getelementbyid( paragraph9 ) ; One can then alter any of the characteristics of the identified object: for example therightparagraph.innerhtml = Some different text ; Another example would be changing the name of a button: say we have a button (in HTML5) <input type="button" id="button1" value="click Me!"> which provides a button with Click Me! written on it, we could change its value (inside a <script> tag) document.getelementbyid("button1").value = "Do not click me again!" ; The different objects in the document object model (everything from paragraphs, <p> to headers <h1> to <h6>, to forms, as well as introduced divisions <div>) have a variety of properties and methods. JavaScript statements. JavaScript has if statements, switch statements, while statements, and for loops. See the www.w3schools.com website for more information on these. Indeed, JavaScript has many more capabilities which are beyond the scope of this introductory document. LSS 2013 5

Additional Information For a detailed description of JavaScript language (i.e. for reference), try D. Flanagan, JavaScript: The definitive Guide 6 th edition, O Reilly 2011. A facility for checking JavaScript syntax may be found at http://www.javascriptlint.com/online_lint.php LSS 2013 6