JavaScript – Part IV Event Handlers, Images, Window object William Pegram George Mason University June 3, 2010.

Slides:



Advertisements
Similar presentations
Java Script Session1 INTRODUCTION.
Advertisements

The Web Warrior Guide to Web Design Technologies
Chapter 7 © 2001 by Addison Wesley Longman, Inc. 1 Chapter 7 Sebesta: Programming the World Wide Web.
Chapter 16 Dynamic HTML and Animation The Web Warrior Guide to Web Design Technologies.
JavaScript 101 Lesson 5: Introduction to Events. Lesson Topics Event driven programming Events and event handlers The onClick event handler for hyperlinks.
Web Development & Design Foundations with XHTML Chapter 14 Key Concepts.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 Chapter 14 Key Concepts 1 Copyright © Terry Felke-Morris.
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.,
INTRODUCTION TO CLIENT-SIDE WEB PROGRAMMING ACM 511 ACM 262 Course Notes.
UNIT 6 JAVASCRIPT EVENT HANDLERS &WEB BROWSERS DETECTION.
Javascript and the Web Whys and Hows of Javascript.
JavaScript: Control Structures September 27, 2005 Slides modified from Internet & World Wide Web: How to Program (3rd) edition. By Deitel, Deitel,
Server vs Client-side validation. JavaScript JavaScript is an object-based language. JavaScript is based on manipulating objects by modifying an object’s.
Unobtrusive JavaScript
Chapter 5 © 2003 by Addison-Wesley, Inc. 1 Chapter 5 JavaScript and HTML Documents.
Execution Environment for JavaScript " Java Script Window object represents the window in which the browser displays documents. " The Window object provides.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
JavaScript Part 1.
Lesson 19. JavaScript errors Since JavaScript is an interpreted language, syntax errors will usually cause the script to fail. Both browsers will provide.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 Chapter 14 Key Concepts 1 Copyright © Terry Felke-Morris.
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.
Working with Objects Creating a Dynamic Web Page.
CITS1231 Web Technologies JavaScript and Document Object Model.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
Multimedia Data Introduction to JavaScript and Scaleable Vector Graphics (SVG) Dr Sandra I. Woolley
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.
Introduction to JavaScript 41 Introduction to Programming the WWW I CMSC Winter 2004 Lecture 17.
CO1552 Web Application Development HTML Forms, Events and an introduction to JavaScript.
Client-Side Scripting JavaScript.  produced by Netscape for use within HTML Web pages.  built into all the major modern browsers. properties  lightweight,
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
Tutorial 5 Windows and Frames Section B - Working with Frames and Other Objects Go to Other Objects.
Chapter 7: DHTML: Object Model and Collections CIS 275—Web Application Development for Business I.
44238: Dynamic Web-site Development Client Side Programming Ian Perry Room:C48 Extension:7287
Cs332a_chapt10.ppt CS332A Advanced HTML Programming DHTML Dynamic Hypertext Markup Language A term describing a series of technologies Not a stand-a-lone.
Scott Marino MSMIS Summer Session Web Site Design and Authoring Session 8 Scott Marino.
JavaScript, Fourth Edition Chapter 4 Manipulating the Browser Object Model.
By Tharith Sriv. To write a web page you use: HHTML (HyperText Markup Language), AASP (Active Server Page), PPHP (HyperText Preprocessor), JJavaScript,
JavaScript Introduction.  JavaScript is a scripting language  A scripting language is a lightweight programming language  A JavaScript can be inserted.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
Event Handling. Objectives Using event handlers Simulating events Using event-related methods.
Java Script About Java Script Document Object Model Incorporating JavaScript Adding JavaScript to HTML Embedding a Javascript External Scripts Javascript.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 7 TH EDITION Chapter 14 Key Concepts 1 Copyright © Terry Felke-Morris.
Host Objects: Browsers and the DOM
Understanding JavaScript and Coding Essentials Lesson 8.
JavaScript Event Handlers. Introduction An event handler executes a segment of a code based on certain events occurring within the application, such as.
 objects in Client  What is Event?  Event Handlers  programming.
Introduction 1.HTML DOM events allow JavaScript to register different event handlers on elements in an HTML document. 2.Events are normally used in combination.
1 Lesson 6 Introducing JavaScript HTML and JavaScript BASICS, 4 th Edition.
Chapter 10 Dynamic HTML (DHTML) JavaScript, Third Edition.
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
The Web Wizard’s Guide To JavaScript Chapter 4 Image Swapping.
Web Programming Java Script-Introduction. What is Javascript? JavaScript is a scripting language using for the Web. JavaScript is a programming language.
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Chapter 5 Host Objects: Browsers.
Copyright © Terry Felke-Morris Web Development & Design Foundations with HTML5 8 th Edition CHAPTER 14 KEY CONCEPTS 1 Copyright.
ASP.NET Forms.
JavaScript and HTML Simple Event Handling 11-May-18.
Using DHTML to Enhance Web Pages
Web Development & Design Foundations with HTML5
Web Development & Design Foundations with HTML5 7th Edition
14 A Brief Look at JavaScript and jQuery.
JavaScript and HTML Simple Event Handling 19-Sep-18.
The Web Wizard’s Guide To JavaScript
JavaScript: Introduction to Scripting
JavaScript and HTML Simple Event Handling 26-Aug-19.
JavaScript and HTML Simple Event Handling 4-Oct-19.
Presentation transcript:

JavaScript – Part IV Event Handlers, Images, Window object William Pegram George Mason University June 3, 2010

When is JavaScript Executed? As noted earlier, JavaScript between tags is executed when the page loads A notable exception to this rule is that the definition of a function is generally placed between tags; however the function is executed only when it is called However, often JavaScript is executed in response to events that occur after the page loads, generally in response to user events

Event Handlers Event /Event HandlerWhat it Handles onabortUser aborted loading the page onblurThe user left the object onchangeThe user changed the object onclickThe user clicked the object onerrorThe script encountered an error onfocusThe user made an object active onloadThe object finished loading onmouseoverThe cursor moved over an object onmouseoutThe cursor moved off the object onselectThe user selected the contents of the object

Event Handlers (cont.) Event/Event handlerWhat it handles onsubmitThe user submitted a form onunloadThe user left the page

Simple Image Rollover Add onmouseover and onmouseout event handlers to tag that change src property of img tag img tag is given a name so that JavaScript can refer to it

Simple Image Rollover Code <a href="" onmouseover="document.arrow.src = 'arrowover.gif'" onmouseout="document.arrow.src= 'arrowup.gif'">

Window Object The screen property of the Window object provides information about the user's screen; in particular, you can detect the user's screen resolution and then display a page appropriate to this screen resolution The location property of the Window object can be used for automatic redirection to another page

Resolution Detection and Redirection Example if (window.screen.width>=1280) window.location="pageover1280.html"; else if (window.screen.width<1280) window.location="pageunder1280.html";

navigator property of the Window object The navigator property of the Window object refers to a Navigator object. This contains information about the user's browser appName property contains the name of the web browser appVersion contains the version information about the browser

Browser/version detection My version of IE 8 returns the following: appName: Microsoft Internet Explorer appVersion: 4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2;.NET CLR ;.NET CLR ;.NET CLR ; Media Center PC 6.0; InfoPath.2;.NET CLR )

Browser/version detection (cont.) My version of Firefox returns the following: appName: Netscape appVersion: 5.0 (Windows; en-US)

Extracting information from Navigator properties As the above examples suggest, the information provided by Navigator properties is often more detailed than needed, so often methods such as parseInt and indexOf are used to get the necessary information e.g. version = parseInt(navigator.appVersion) would extract the integer at the beginning of these appVersion values

indexOf String method string1.indexOf(string2) This method returns the location of the first occurrence of string2 within string1, where the first position of a string is position 0 if string2 is not contained within string1, the method returns -1

Using appName in browser detection If we had to test whether appName was equal to a certain string, like "Microsoft Internet Explorer", we would have to get the test exactly right and would have to modify the code if different versions of the browser, now or in the future, would return different values for appName Using indexOf allows a more flexible test – how? Answer shown on next page

indexOf example if (window.navigator.appName.indexOf("Micro soft")!= -1) window.location="ieversionofpage.html"; else if (window.navigator.appName.indexOf("Netsca pe")!=-1) window.location="firefoxversionofpage.html" ;

Alternatives to browser and version detection One could do browser and version detection because a particular JavaScript feature will produce an error in a version of a browser So one could do a series of tests for browser and version to redirect to pages that wouldn't produce errors However, there are two other approaches to use to avoid such problems

Alternatives to browser and version detection (cont.) Using the language attribute of the tag -- means that the code between these script tags will not be executed by browsers that only run JavaScript 1.0 or 1.1 One should set the language attribute at the minimum level necessary to avoid errors in that portion of code

Feature Testing as an Alternative A more direct, simpler way to deal with browser differences in support of JavaScript is to test whether the particular feature is supported. For example, to determine whether a particular feature is supported, we simply test for it in an if statement to decide whether to use it

Feature Testing examples Before using the split() method, we write if (s.split) /*Check to see whether it exists without invoking it */ a = s.split(":"); /*If it does exist, then safe to invoke it */ else a= mysplit(s,":"); // use some alternative

Feature Testing examples (cont.) if (document.images) {} //ok to use image rollovers Thus we are directly testing for what we need to use rather than worrying about a list of browsers and version numbers which would need to be updated over time