JavaScript and Ajax (JavaScript Environment) Week 6 Web site:

Slides:



Advertisements
Similar presentations
Chapter 08: Adding Adding Interactivity Interactivity With With Behaviors Behaviors By Bill Bennett Associate Professor MSJC CIS MVC.
Advertisements

JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
Computer and Communication Fundamental Basic web programming Lecture 8 Rina Zviel-Girshin.
HTML 5 and CSS 3, Illustrated Complete Unit L: Programming Web Pages with JavaScript.
The Web Warrior Guide to Web Design Technologies
JavaScript (Part 2) CS 183 4/13/2010. JavaScript (Part 2) Will cover: ◦ For.. In ◦ Events ◦ Try.. Catch, Throw ◦ Objects.
Javascript Document Object Model. How to use JavaScript  JavaScript can be embedded into your html pages in a couple of ways  in elements in both and.
20-Jun-15 JavaScript and HTML Simple Event Handling.
Computer Science 103 Chapter 4 Advanced JavaScript.
JavaScript 101 Lesson 5: Introduction to Events. Lesson Topics Event driven programming Events and event handlers The onClick event handler for hyperlinks.
JavaScript, Third Edition
Introduction To JavaScript What JavaScript Is: a general purpose scripting language that allows a static page to interact with users and respond to events.
Inline, Internal, and External FIle
1 Events Lect 8. 2 Event-driven Pages one popular feature of the Web is its interactive nature e.g., you click on buttons to make windows appear e.g.,
Javascript and the Web Whys and Hows of Javascript.
4.1 JavaScript Introduction
CNIT 133 Interactive Web Pags – JavaScript and AJAX Event and Event Handling.
JavaScript - a Brief Introduction Anupriya. What is JavaScript Object based (not object oriented) programming language –very limited object creation –a.
Server vs Client-side validation. JavaScript JavaScript is an object-based language. JavaScript is based on manipulating objects by modifying an object’s.
JavaScript II ECT 270 Robin Burke. Outline JavaScript review Processing Syntax Events and event handling Form validation.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
Internet Mark-up Languages CO32036 Part Lecture: Elementary JavaScript.
1 JavaScript. 2 What’s wrong with JavaScript? A very powerful language, yet –Often hated –Browser inconsistencies –Misunderstood –Developers find it painful.
DHTML: Dynamic HTML Internet Technology1. What is DHTML? A collection of enhancements to HTML ► To create dynamic and interactive websites Combination.
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.
JavaScript: Functions © by Pearson Education, Inc. All Rights Reserved.
Lesson13. JavaScript JavaScript is an interpreted language, designed to function within a web browser. It can also be used on the server.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
JavaScript - A Web Script Language Fred Durao
Lecture 10 JavaScript: DOM and Dynamic HTML Boriana Koleva Room: C54
JavaScript - Basic Concepts Prepared and Presented by Hienvinh Nguyen, Afshin Tiraie.
44238: Dynamic Web-site Development Client Side Programming Ian Perry Room:C48 Extension:7287
Event JavaScript's interaction with HTML is handled through events that occur when the user or browser manipulates a page. When the page loads, that is.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Jaana Holvikivi 1 Introduction to Javascript Jaana Holvikivi Metropolia.
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
 Scripts that execute in response to some event  User clicking on something  Script does NOT execute as part of page loading  DOM facilities like.
Rich Internet Applications 3. Client JavaScript. Document Object Model (DOM) The DOM, is an interface that allows scripts or programs to access and manipulate.
CIS 3.5 Lecture 2.3 "Introduction to JavaScript".
JS DOM. Introduction An HTML page is rendered (painted) in a browser The browser assembles all the elements (objects) contained in the HTML page To create.
Understanding JavaScript and Coding Essentials Lesson 8.
JavaScript II ECT 270 Robin Burke. Outline Functions Events and event handling Form validation.
JavaScript Event Handlers. Introduction An event handler executes a segment of a code based on certain events occurring within the application, such as.
LESSON : EVENTS AND FORM VALIDATION -JAVASCRIPT. EVENTS CLICK.
CGS 3066: Web Programming and Design Spring 2016 Programming in JavaScript.
JavaScript Events Java 4 Understanding Events Events add interactivity between the web page and the user You can think of an event as a trigger that.
CSC 121 Computers and Scientific Thinking Fall Event-Driven Programming.
Javascript Basic Concepts Presentation By: Er. Sunny Chanday Lecturer CSE/IT RBIENT.
JavaScript Events. Understanding Events Events add interactivity between the web page and the user Events add interactivity between the web page and the.
Web Programming Java Script-Introduction. What is Javascript? JavaScript is a scripting language using for the Web. JavaScript is a programming language.
SE-2840 Dr. Mark L. Hornick 1 Dynamic HTML Handling events from DOM objects.
JavaScript and HTML Simple Event Handling 11-May-18.
Week 4: Introduction to Javascript
Intro to JavaScript CS 1150 Spring 2017.
Web Development & Design Foundations with HTML5 7th Edition
JavaScript and HTML Simple Event Handling 19-Sep-18.
DHTML Javascript Internet Technology.
Your 1st Programming Assignment
Events Comp 205 Fall 2017.
DHTML Javascript Internet Technology.
Functions, Regular expressions and Events
JavaScript and Ajax (JavaScript Events)
CNIT 133 Interactive Web Pags – JavaScript and AJAX
Web Programming and Design
Week 5: Recap and Portfolio Site
Web Programming and Design
JavaScript and HTML Simple Event Handling 26-Aug-19.
JavaScript and HTML Simple Event Handling 4-Oct-19.
Presentation transcript:

JavaScript and Ajax (JavaScript Environment) Week 6 Web site:

JavaScript Environment To understand client-side JavaScript, you must understand the programming environment provided by a web browser. Three important features of that programming environment:  The Window object that serves as the global object and global execution context for the client-side JavaScript code  The client-side object hierarchy and the Document Object Model (DOM) that forms a part of it  The event-driven programming model

The Window as Global Execution Context Window object represents the browser window (or frame) that displays the document. The Window object is the global object in client-side programming The window object defines a number of properties and methods that allow you to manipulate the web browser window. It also defines the document property for the Document object. It has two self-referential properties, window and self. You can use either global variable to refer directly to the Window object. Since the Window object is the global object in client-side JavaScript, all global variables are defined as properties of the window. The following 2 lines of code are the same: var answer = 42; // declare and initialize a global variable window.answer = 42; // create a new property of the Window obj

The Client-Side Object Hierarchy and the DOM The Window object is the key object in client-side JavaScript. All other client-side objects are accessed via this object. (document property; location property, history property, navigator property) The Document object (and other client-side JavaScript objects) also have properties that refer to other objects. (Document object has a forms[ ] array containing Form objects, links[] array containing Link objects, anchors[] array containing Anchor objects) To refer to one of these forms: window. document.forms[0] To continue with the example: window.document.forms[0].elements[3].options[2].text /* returns the text of an option */ Many of the objects descend from the Document object. This subtree of the larger client-side object hierarchy is known as the document object model (DOM).

The Event-Driven Programming Model In client-side JavaScript, the web browser notifies programs of user input by generating events. (e.g. keystroke events, mouse motion events, etc…) When an event occurs, the web browser attempts to invoke an appropriate event handler function to respond to the event.

The Role of JavaScript on the Web Web browsers display HTML-structured text styled with CSS stylesheets. HTML defines the content, and CSS supplies the presentation. Properly used, JavaScript adds behavior to the content and its presentation. The role of JavaScript is to enhance a user’s browsing experience, making it easier to obtain or transmit information.

Embedding Scripts in HTML Client-Side JavaScript code is embedded within HTML documents in a number of ways:  Between a pair of and tags JavaScript codes.  From an external file specified by the src attribute of a tag  In an event handler, specified as the value of an HTML attribute such as onclick or onmousover Click me to invoke onclick event.  In an URL that uses the special javascript: (JavaScript Pseudo Protocol) javascript: alert("Call from JavaScript Pseudo Protocol");

The Tag Client-side JavaScript scripts are part of an HTML file and are coded within and tags: // your JavaScript code goes here A simple JavaScript program in an HTML file: Today’s date function print_todays_date() { var d = new Date(); document.write(d.toLocaleString()); } The date and time are: print_todays_date();

Scripts in External Files The tag supports a src attribute that specifies the URL of a file containing JavaScript code: print_todays_date(); A JavaScript file typically has a.js extension and contains pure JavaScript, without tags or any other HTML. Inside the util.js file: function print_todays_date() { var d = new Date(); document.write(d.toLocaleString()); }

Event Handlers in HTML More dynamic programs define event handlers that are automatically invoked by the web browser when certain events occur. Because events in client-side JavaScript originate from HTML objects (such as buttons), event handlers can be defined as attributes of those objects: These are the most common event handlers:  onclick  onmousedown, onmouseup  onmouseover, onmouseout  onchange  onload

JavaScript in URLs Another way that JavaScript code can be included on the client side is in a URL following the javascript: pseudo protocol specifier. This string of JavaScript code is treated as a single line of code, which means that statements must be separated by semicolons and that /* */ comments must be used in place of // comments: javascript: var now = new Date(); alert("The time is: " + now); The browser executes the JavaScript code contained in the URL and uses the value of the last JavaScript statement or expression, converted to a string, as the contents of the new document to display. This string value may contain HTML tags and is formatted and displayed just like any other document loaded into the browser. JavaScript URLs may also contain JavaScript statements that perform actions but return no value: javascript: alert("Hello World!")

JavaScript in URLs (continue…) To use a JavaScript URL to execute some JavaScript code without altering the currently displayed document, be sure that the last statement in the URL has no return value. Or use the void operator to explicitly specify an undefined return value: javascript: window.open("about:blank"); void (0); You can use a JavaScript URL anywhere you would use a regular URL. One handy way to use this syntax is to type it directly into the location field of your browser, where you can test arbitrary JavaScript code without having to open your editor and create an HTML file containing the code.

Execution of JavaScript Programs The JavaScript code in tags is executed as part of document loading and parsing process. Document loading process:  Load document (execute )  Parse document (text output from will be displayed in document)  Load image, sound, etc…  Execute onload event (browser fires off this event)  JavaScript Event-driven phase and JavaScript URLs (mouse motion, mouse click, key press, etc…)  Execute onunload event Note: when the onload handler is triggered, the document is fully loaded and parsed. Therefore, onload event handler must not call document.write(). It will overwrite the current document.