CSC Javascript

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

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

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

Introduction to DHTML

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

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

COMP519 Web Programming Lecture 16: JavaScript (Part 7) Handouts

INDEX SYMBOLS See also

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

Fundamentals of Website Development

Contents. Demos folder: Demos\14-Ajax. 1. Overview of Ajax. 2. Using Ajax directly. 3. jquery and Ajax. 4. Consuming RESTful services

DOM Primer Part 2. Contents

TEXTAREA NN 2 IE 3 DOM 1

JavaScript Introduction

New Perspectives on Creating Web Pages with HTML. Tutorial Objectives

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

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

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

Events: another simple example

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

An Introduction to AJAX. By : I. Moamin Abughazaleh

CE212 Web Application Programming Part 2

Ajax. Ronald J. Glotzbach

JavaScript Handling Events Page 1

Name Related Elements Type Default Depr. DTD Comment

Princeton University COS 333: Advanced Programming Techniques A Subset of JavaScript

710 Index Attributes, 127 action attribute, 263 assigning, bottom attribute, domain name attribute, 481 expiration date attribute, 480 8

Princeton University COS 333: Advanced Programming Techniques A Subset of JavaScript

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

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

JSF - H:INPUTSECRET. Class name of a validator that s created and attached to a component

XMLHttpRequest. CS144: Web Applications

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

Ajax. David Matuszek's presentation,

CS Final Exam Review Suggestions - Spring 2018

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

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

HTML and JavaScript: Forms and Validation

Photo from DOM

Installation and Configuration Manual

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

Dynamic HTML becomes HTML5. HTML Forms and Server Processing. Form Submission to Web Server. DHTML - Mouse Events. CMST385: Slide Set 8: Forms

Q1. What is JavaScript?

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

Web Designing Course

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

Session 16. JavaScript Part 1. Reading

JavaScript Programming

The attributes property of an element node returns a list of attribute nodes. It s called a named node map

AJAX: Rich Internet Applications

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

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

UNIT - III. Every element in a document tree refers to a Node object. Some nodes of the tree are

Canvas & Brush Reference. Source: stock.xchng, Maarten Uilenbroek

JSF - H:PANELGRID. JSF Tag. Rendered Output. Tag Attributes. The h:panel tag renders an HTML "table" element. Attribute & Description.

CICS 515 a Internet Programming Week 3. Mike Feeley

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

Session 6. JavaScript Part 1. Reading

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

HTML 5 Tables and Forms

Client-side Techniques. Client-side Techniques HTML/XHTML. Static web pages, Interactive web page: without depending on interaction with a server

Beijing , China. Keywords: Web system, XSS vulnerability, Filtering mechanisms, Vulnerability scanning.

Document Object Model. Overview

Place User-Defined Functions in the HEAD Section

JavaScript Specialist v2.0 Exam 1D0-735

Introduction to using HTML to design webpages

INLEDANDE WEBBPROGRAMMERING MED JAVASCRIPT INTRODUCTION TO WEB PROGRAMING USING JAVASCRIPT

Introduction to JavaScript, Part 2


JavaScript s role on the Web

Using JavaScript for Client-Side Behavior


Javascript Lecture 23

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

5. Strict mode use strict ; 6. Statement without semicolon, with semicolon 7. Keywords 8. Variables var keyword and global scope variable 9.

Web Design and Application Development

JSF - H:SELECTONERADIO

Notes General. IS 651: Distributed Systems 1

CSE 115. Introduction to Computer Science I

Continues the Technical Activities Originated in the WAP Forum

Session 17. JavaScript Part 2. W3C DOM Reading and Reference. Background and introduction.

5/19/2015. Objectives. JavaScript, Sixth Edition. Understanding the Browser Object Model and the Document Object Model. Objectives (cont'd.

Ajax HTML5 Cookies. Sessions 1A and 1B

JSF - H:SELECTONEMENU

CSI 3140 WWW Structures, Techniques and Standards. Browsers and the DOM

JAVASCRIPT FOR PROGRAMMERS

JavaScript 3. Working with the Document Object Model (DOM) and DHTML

Web Development & Design Foundations with HTML5

Module7: AJAX. Click, wait, and refresh user interaction. Synchronous request/response communication model. Page-driven: Workflow is based on pages

COMS W3101: SCRIPTING LANGUAGES: JAVASCRIPT (FALL 2017)

INTRODUCTION TO WEB DEVELOPMENT AND HTML. Lecture 15: JavaScript loops, Objects, Events - Spring 2011

A.A. 2008/09. What is Ajax?

ajax1.html 1/2 lectures/7/src/ ajax1.html 2/2 lectures/7/src/

Events & Callbacks (ESaaS 6.5)! 2013 Armando Fox & David Patterson, all rights reserved

CITS1231 Web Technologies

Experience the Magic of On-the-fly Modernization. Screen Customization Guide. for Genie Version 3.0

Fall Semester (081) Module7: AJAX

Project 3 Web Security Part 1. Outline

Rich User Interfaces with KSS

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

Transcription:

CSC 4800 Javascript

See book! Javascript Syntax

How to embed javascript between <script></script> from an external file In an event handler URL - bookmarklet <script language= javascript > <![CDATA[ // javascript code ]]> </script> <script src= myfile.js > </script> <input onclick= foo(); />

Timers id = settimeout(code, delay) cleartimeout(id) id = setinterval(code, interval) clearinterval(id)

When is javascript executed? Code can be embedded at various places in the document. It executes when that section of the document is rendered. This is the one time you can use document.write() without erasing the current document! More frequently, code is attached to an event handler. It runs when the event occurs.

Naming document objects <form name= f1 > </form> document.forms[0] // by position document.forms.f1 // by name document.forms[ f1 ] // array index As we will shortly see, we will normally use other methods to access pieces of a document.

DOM

Interfaces Javascript defines interfaces for: document createattribute(), createelement(), createtextnode() getelementbyid(), getelementsbytagname() node appendchild(), removechild(), replacechild() insertbefore() element getattribute(), setattribute(), removeattribute() getelementsbytagname() This list is not exhaustive!

DOM Levels DOM Level 1, October 1998. defines core DOM interfaces, such as Node, Element, Attr, and Document All modern browsers DOM Level 2, November 2000. required core module has Document, Node, Element, and Text interfaces (and others) events, css Firefox, Safari, Opera IE supports some level 2, like CSS; but not events

innerhtml property more convenient than createelement(), appendchild()

scripting CSS Set the style directly change the class change the stylesheet

Javascript Events Three event models original event model standard event model IE event model

Basic Event Handling Events and Event Types onblur onchange onclick, ondblclick onkeydown, onkeypress, onkeyup onmouseover, onmousedown, onmousemove, onmouseout, onmouseup onfocus onload onsubmit etc. Device-dependent vs. device-independent events

Event Handlers as Attributes <input type= button onclick= alert( pressed ); /> Usually better to call a function: <form onsubmit= return formvalidate() >

Event Handlers as Properties document.f1.b1.onclick=myfunction; Note that the line is NOT: document.f1.b1.onclick=myfunction(); Advantages (vs attributes) reduces intermingling of HTML and js dynamic Disadvantage separates handler from element

Event Handler Return Values return false to prevent default action For example, form validation.

Event Handlers and this handler is a method of the element that generated the event. button.onclick = o.mymethod; Inside mymethod, this refers to button, not to o. You can do this: button.onclick = function() {o.mymethod();}

Javascript & forms Every form element has a name attribute used for form submission: <input type= text name= lastname /> <form> also has a name attribute Not used for form submission But can be used with javascript <form name= studentform > can be accessed like document.studentform and the fields can be accessed like document.studentform.lastname

Form element properties type (read-only) form (read-only) name (read-only) value (read/write) string sent to server when form is submitted. text, textarea: text entered by user radio, checkbox: value not seen by user a string set in html

Ajax Using XMLHttpRequest create XMLHttpRequest object specify and submit HTTP request to server (usually) asynchronously retrieve response

Creating a request object // This is a list of XMLHttpRequest creation factory functions to try HTTP._factories = [ function() { return new XMLHttpRequest(); }, function() { return new ActiveXObject("Msxml2.XMLHTTP"); }, function() { return new ActiveXObject("Microsoft.XMLHTTP"); } ]; // When we find a factory that works, store it here HTTP._factory = null; // Create and return a new XMLHttpRequest object. // // The first time we're called, try the list of factory functions until // we find one that returns a nonnull value and does not throw an // exception. Once we find a working factory, remember it for later use. // HTTP.newRequest = function() { if (HTTP._factory!= null) return HTTP._factory(); for(var i = 0; i < HTTP._factories.length; i++) { try { var factory = HTTP._factories[i]; var request = factory(); if (request!= null) { HTTP._factory = factory; return request; } } catch(e) { continue; } } } // If we get here, none of the factory candidates succeeded, // so throw an exception now and for all future calls. HTTP._factory = function() { throw new Error("XMLHttpRequest not supported"); } HTTP._factory(); // Throw an error

Sending/Handling async response function makerequest() { var request = HTTP.newRequest(); request.onreadystatechange = function() { if (request.readystate == 4) { // if request is finished if (request.status == 200) { // if it was successful Using Ajax from html: <h2 onclick="makerequest()">simple Ajax</h2> alert(request.responsetext); } } } request.open("get", 'getcountries'); } request.send(null);

Javascript Libraries Prototype - http://www.prototypejs.org/ script.aculo.us

Prototype: Ajax.Updater In the layout: <%= javascript_include_tag :defaults %> <%= stylesheet_link_tag "ajaxtest" %> javascript: <script type="text/javascript"> //<![CDATA[ function makerequest() { new Ajax.Updater('cresult','getcountries', { method: 'get', parameters: {name: $('name').value} }); return false; // don t submit the form! } //]]> </script>

Prototype: Ajax.Updater HTML: <h2>prototype Ajax Example</h2> <form action="#" onsubmit="return makerequest()" > <input type="text" name="name" id="name" /> <input type="submit"> </form> <div id="cresult"></div>

Prototype: Ajax.Updater getcountries.rhtml <ul> <% @countries.each do c %> <li><%= c.name %></li> <% end %> </ul> getcountries in controller def getcountries end name = params['name'] 'G'; @countries = Country.find(:all, :conditions => render :layout => false ["name ilike?", name+'%'])

script.aculo.us autocomplete View <%= text_field 'user', 'country' %> <br/> <div id="user_country_auto_complete" class="auto_complete"> </div> <%= auto_complete_field :user_country, :url=>{:action=>'autocomplete_countries'} %> autocomplete_countries.rhtml <ul class="autocomplete_list"> <% @countries.each do c %> <li class="autocomplete_item"><%= c.name %></li> <% end %> </ul>

script.aculo.us autocomplete controller code for autocomplete def autocomplete_countries end @countries = Country.find(:all, :conditions => ["name ilike?", params[:user][:country]+'%']) render :layout => false