Chapter 19: Adding JavaScript

Slides:



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

JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.
Session 11 Dynamic HTML: Event Model and Filters Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
The Web Warrior Guide to Web Design Technologies
NAMEd anchors Enabling users to jump to specific points within Web documents.
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.
Macromedia Dreamweaver 4 Advanced Level Course. Add Rollovers Rollovers or mouseovers are possibly the most popular effects used in designing Web pages.
NAMEd anchors Enabling users to jump to specific points within Web documents.
Chapter 9 Introduction to the Document Object Model (DOM) JavaScript, Third Edition.
Lesson 2 Event Handling. Object Event Handlers Most of the objects that make up the Document Object Model respond to asynchronous, user generated events.
JavaScript Client Side scripting. Client-side Scripts Client-side scripts, which run on the user’s workstation can be used to: Validate user inputs entered.
LEARN THE QUICK AND EASY WAY! VISUAL QUICKSTART GUIDE HTML and CSS 8th Edition Chapter 2: Working with Webpage Files.
UNIT 6 JAVASCRIPT EVENT HANDLERS &WEB BROWSERS DETECTION.
JS: DOM Form Form Object Form Object –The Form object represents an HTML form. –For each instance of a tag in an HTML document, a Form object is created.
HTML Forms/Events (Instructor Lesson) The Event model HTML Forms Custom Events 1.
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.
McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Dynamic Action with Macromedia Dreamweaver MX Barry Sosinsky Valda Hilley.
Event Handlers CS101 Introduction to Computing. Learning Goals Learn about event handlers Determine how events are useful in JavaScript Discover where.
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.
1 JavaScript: Event Model November 1, 2005 Slides modified from Internet & World Wide Web: How to Program (3rd) edition. By Deitel, Deitel, and Goldberg.
10 Adding Interactivity to a Web Site Section 10.1 Define scripting Summarize interactivity design guidelines Identify scripting languages Compare common.
Internet Mark-up Languages CO32036 Part Lecture: Elementary JavaScript.
DOM and JavaScript Aryo Pinandito.
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.
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.
CO1552 Web Application Development HTML Forms, Events and an introduction to JavaScript.
Scripts Chapter Scripts Small programs used to add interactivity to your web pages The backbone of Dynamic HTML (DHTML) Most scripts are written.
JavaScript - A Web Script Language Fred Durao
Intro to JavaScript Scripting language –ECMAScript compliant –Client side vs. server side Syntactic rules –White space –Statement end: ; optional –Comments:
Lecture 10 JavaScript: DOM and Dynamic HTML Boriana Koleva Room: C54
Chapter 14: Dynamic HTML: Event Model Presented by: Colbie Brown CS340 Java Web Development Dr. Gloria Carter Love.
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.
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,
Chapter 19 Creating and Processing HTML Forms. How HTML Forms Transmit Data name1=value1&name2=value2...&nameN =valueN GET or POST GET, an HTTP GET request,
Thursday, August 6 th, 2015 Instructor: Craig Duckett Event Handling.
1 Chapter 05: Creating Your Form. What is a Form? When you visit some websites you see a form. With this form, you are asked to complete, for example,
 2004 Prentice Hall, Inc. All rights reserved. 1 Segment – 4 Dynamic HTML & CSS.
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.
Event Handling. Objectives Using event handlers Simulating events Using event-related methods.
Project 8: Reacting to Events Essentials for Design JavaScript Level One Michael Brooks.
Basic Webpage Design Mark-up html document with images.
COS 125 DAY 20. Agenda Assignment 8 not corrected yet Assignment 9 posted  Due April 16 New course time line Discussion on Scripts 
Web Programming Java Script & jQuery Web Programming.
HTML FORMS The TEXT Object Presented By: Ankit Gupta.
CIS 3.5 Lecture 2.3 "Introduction to JavaScript".
7. JavaScript Events. 2 Motto: Do you think I can listen all day to such stuff? –Lewis Carroll.
JavaScript Events.
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.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
CIS 4004: Web Based IT (JavaScript – Part 1) Page 1 © Dr. Mark Llewellyn CIS 4004: Web Based Information Technology Fall 2012 JavaScript – Part 1 Department.
Functions Wouldn’t it be nice to be able to bring up a new animal and paragraph without having to reload the page each time? We can! We can place the.
SE-2840 Dr. Mark L. Hornick 1 Dynamic HTML Handling events from DOM objects.
Section 10.1 Define scripting
JavaScript and HTML Simple Event Handling 11-May-18.
Week 4: Introduction to Javascript
JavaScript and HTML Simple Event Handling 19-Sep-18.
JavaScript Events.
Web Design and Development
JavaScript and Ajax (JavaScript Events)
CNIT 133 Interactive Web Pags – JavaScript and AJAX
Web Programming and Design
JavaScript and HTML Simple Event Handling 26-Aug-19.
JavaScript and HTML Simple Event Handling 4-Oct-19.
Presentation transcript:

Chapter 19: Adding JavaScript HTML and CSS 8th Edition Chapter 19: Adding JavaScript

Objectives Work with scripts. Use JavaScript libraries. Load an external script. Minify a script. Add an embedded script. Handle JavaScript events.

Adding JavaScript JavaScript defines special behaviors for a webpage. JavaScript programs can: Show and hide content. Write programs that load data and dynamically update page. Build carousels and slideshows like those on news sites. Drive custom HTML5 audio and video element controls. Create games that use HTML5's canvas element. Write full-blown web applications that leverage features in HTML5 and related technologies.

JavaScript Libraries JavaScript libraries add simple interactivity and sophisticated behavior to pages. Help pages behave consistently across browsers. jQuery enjoys the most widespread use among JavaScript libraries. Beginners find it easy to learn, it has good online documentation, and it has a large community behind it.

Loading an External Script Two primary kinds of scripts: External scripts load from an external file. Embedded scripts are embedded in page. Better to load scripts from an external file than to embed them in your HTML. A single JavaScript file can be loaded by each page that needs it. Script element is used to both load an external script or embed a script.

To Load an External Script Type <script src="script.js"></script>. Where script.js is the location on the server and the file name of the external script. Place each script element directly before the </body> end tag whenever possible, instead of in the document's head element. Tips To keep your files organized, it’s common to place your JavaScript files in a sub-folder (js and scripts are popular names); see “Organizing Files” in Chapter 2. Your src attribute values would need to reflect this, just like any URL that points to a resource. For instance, if the JavaScript file referenced were in a folder named js that is itself in a folder named assets, you could type <script src="assets/js/behavior.js"></script>. (That’s just one example; there are other ways to represent the URL. See “URLs” in Chapter 1.) A sample piece of JavaScript is shown. Because JavaScript is just text, you can write it in the same editor you use to create your HTML and CSS. If this example were saved in a file named behavior.js, it would load into the pages shown. Your page may load multiple JavaScript files and contain multiple embedded scripts. By default, browsers will load scripts (when necessary) and execute scripts in the order in which they appear in your HTML. See the “Scripting and Performance Best Practices” sidebar to learn why you should avoid multiple scripts when possible and how to minify them. You can specify any valid file names you’d like for your external scripts as long as they have the .js extension. It’s customary to give minified scripts a .min.js extension so you can distinguish easily between the normal files and the condensed ones. Keep both files on hand—update your scripts in the normal file (because it’s easier for you to read), but use the minified version on your site (because it’s faster for the browser). And don’t forget to generate a new minified file when you update your script; otherwise, visitors will get the old version. If you create a minified file, be sure to change the reference to your script in your HTML. For example, <script src="behavior.min.js"></script>. Otherwise, your page will continue to load the normal file and your visitors won’t reap the benefits of the smaller file. You can change the src back to the normal file name while you’re working on changes to your script. Browsers that don’t understand JavaScript (these are admittedly rare) or that have it disabled by the user will ignore your JavaScript file. So be sure that your page doesn’t rely on JavaScript to provide users access to its content and basic experience. (Web applications that rely heavily on JavaScript are often an exception.) Technically, there is a third way to add JavaScript to a page: inline scripts. An inline script is a small bit of JavaScript assigned to certain element attributes directly in your HTML. I hesitate to mention them except to point out that you should avoid using them, just as you would avoid inline style sheets. Just as inline style sheets mix your HTML and CSS, inline scripts inextricably intertwine your HTML and JavaScript, rather than keeping them separate per best practices.

Scripting and Performance Best Practices Browser handles scripts by downloading (for external scripts), parsing, and executing scripts in order they appear in HTML. Blocking behavior is where the browser neither downloads nor renders any content appearing after the script element. To make JavaScript non-blocking, put all script elements at end of HTML, right before </body> end tag.

Minify Your Script Minify is a quick way to speed up script loading by combining all JavaScript into single file (or into as few as possible) and minify the code. Minified code doesn't have line breaks, comments, or extra whitespace (among other possible differences from un-minified code). Tools to minify scripts: Google Closure Compiler UglifyJS YUI Compressor

Adding an Embedded Script Embedded script is script that exists in the HTML doc, much like an embedded style sheet. Contained in a script element. Lacks a src attribute. Embedding script is not preferred, but sometimes necessary. Embed script directly before </body> end tag whenever possible. Possible, but less desirable to embed script in head.

To Add an Embedded Script In HTML document, type <script>. Type the content of the script. Type </script>. Tip Each script element is processed in the order in which it appears in the HTML, whether it’s an embedded script or an external one (see “Loading an External Script”). Even though the script element requires an end tag (</script>), you cannot embed code between it and the start tag when a src attribute is present (see “Loading an External Script”). In other words, <script src="your-functions.js">Some other functions in here</script> is invalid. Any given script element may either load an external script with src, or embed a script and not have a src.

JavaScript Events JavaScript events are specific, predefined events triggered by visitor or browser. Partial list of JavaScript events: onblur: Visitor leaves element that was previously in focus. onchange: Visitor modifies value or contents of element. onclick: Visitor clicks specified area or hits Return or Enter key while focused on it (like on a link). ondblclick: Visitor double-clicks specified area. onfocus: Visitor selects, clicks, or tabs to specified element. onkeydown: Visitor presses down on a key while in specified element.

Handling JavaScript Events Partial list of JavaScript events: onkeypress: Visitor presses down and lets go of a key while in specified element. onkeyup: Visitor lets go of key after typing in specified element. onload: Browser finishes loading page, including all external files (images, style sheets, JavaScript, and so on). onmousedown: Visitor presses mouse button down over specified element. onmousemove: Visitor moves mouse cursor. onmouseout: Visitor moves mouse away from specified element after having been over it.

Handling JavaScript Events Partial list of JavaScript events: onmouseover: Visitor points mouse at element. onmouseup: Visitor lets mouse button go after having clicked the element (the opposite of onmousedown). onreset: Visitor clicks form's reset button or presses Return or Enter key while focused on the button. onselect: Visitor selects one or more characters or words in the element. onsubmit: Visitor clicks form's submit button or presses Return or Enter key while focused on the button.