Download presentation
Presentation is loading. Please wait.
1
HTML Level II (CyberAdvantage)
Session II Chapter 15 – How to Use JavaScript and jQuery to Enhance your Web Page
2
Class Outline jQuery Effects jQuery HTML jQuery Traversing jQuery AJAX
Intro to JavaScript Object Technology Document Object Model (DOM) JavaScript Frameworks Introduction to jQuery jQuery Syntax jQuery Selectors jQuery Events jQuery Effects jQuery HTML jQuery Traversing jQuery AJAX jQuery Misc 11/15/2018 Copyright © Carl M. Burnett
3
How JavaScript fits into Web Architecture
Client Devices Client Request to Web Server HTML Page Embedded JavaScript External JavaScript File Cloud Web Server Server Response to Client with Embedded or External JavaScript File The web architecture is based on client devices, a web server and the cloud. When a client device request a web page from a web server the server processes this request. If the request include some actionable events to happen in the web page, like a image swap or form validation, to take some of this processing demand off the server the web page designer will embed in the web page, some scripting programming code or attach a external file with the scripting programming code. This is then send to the client for processing on the client. The preferred scripting language to be used under based on the new foundation of HTML5, is JavaScript. All browsers today include a JavaScript interpreter that runs the JavaScript language to interpret the code. 11/15/2018 Copyright © Carl M. Burnett
4
JavaScript for Current Date and Year
var today = new Date(); document.write("Current date: "); document.write(today.toDateString()); </script> </p> document.write("© "); document.write(today.getFullYear()); document.write(", San Joaquin Valley Town Hall") Example 11/15/2018 Copyright © Carl M. Burnett
5
Object Oriented Programming Concepts
Class Objects Inheritance Data Encapsulation Metadata Polymorphism
6
Class Characteristics
Class = Containers Class = Structure Glass Plastic Metal Class is Instantiated State Structure Behaviors Hierarchy Access Class = Behaviors Pour Fill Shake Class = Hierarchy Class = Access 1. Private 2. Protected 3. Public
7
Objects Data Structures Properties Methods
8
Inheritance Object or Class is based on another Object
Multiple Inheritance Multilevel Inheritance Single Inheritance Hybrid Inheritance Hierarchical Inheritance
9
Data Encapsulation Metadata – Data about Data Record House No. Street
Street Type City State Address 123 Post Avenue Westbury New York Record Salutation First Name MI Last Name Suffix Person Ms. Sally M Smith III Record
10
Polymorphism Polymorphism lets methods react to system behaviors at runtime. Where Can I buy you? At the Target Store around the corner. How Much Do You Cost? $12
11
The Document Object Model (DOM) for a Web Page
Objects Properties Methods Inheritance Data Encapsulation HTML <html> </html> Head <head> </head> Body <body> </body> Title <title> </title> section <section> </section> h1 <h1> </h1> ul <ul> </ul> p <p> </p> Text li <li> </li> What is important to understand is the Document Object Model. But first let review what makes up an object. In Chapter 1 we covered what a object is and what characteristics a object possess. 1. Object posses properties. These can be such things as size, color, and shape. 2. Object posses methods. These can be move, pour, open and close. 3. Objects can inherit properties, method and data from a parent object. 4. And objects can encapsulated data by using metadata. With this understanding about the characteristics about an object we can apply this same understanding to the objects that make up a web page. In this case, the overarching parent object of a web page is the HTML declaration. This object is created with the html opening and closing tags. The tags create the HTML object and all object oriented characteristics of the HTML object can be changed based on triggering events. In order for these characteristics to be changed on the client side, a scripting language must be used. In the case, the standard is JavaScript and this language is interpreted through the JavaScript interpreter engine that is part of all browsers. The next object in the web page document object model is the head. This object is created by the head opening and closing tags. It can inherit any characteristics from its parent object, the HTML object. The next object is the body. This object is created by the body opening and closing tags. It can inherit any characteristics from its parent object, the HTML object. A child object of the Head object is the Title object. This object is created by the title opening and closing tags. It can inherit any characteristics from its parent object, the Head object. The next object is the new HTML5 document object called a section. This object is created by the section opening and closing tags. It can inherit any characteristics from its parent object, the body object. The next objects can be many objects to define the content of body or section or any other object container. These objects inlcude the h1 object, the ul object and the p object. These objects are created by the h1, ul, and p opening and closing tags. That can inherit any characteristics from its parent object, the body object. The last objects are the li objects that are created with the li tags. They can inherit any characteristics from its parent object, the ul object. Each of these object can contain data and for the h1 object, the ul li objects and p object all contain textual data that can be changed with the JavaScript language. 11/15/2018 Copyright © Carl M. Burnett
12
The JavaScript DOM Event Cycle
Page Loaded Event Occurs Script Executes DOM Modified Page Updated 11/15/2018 Copyright © Carl M. Burnett
13
How to Include JavaScript in a HTML Document
Two attributes of the script element Attributes Description src Location and Name of External JavaScript File. type HTML5 (Omit) - otherwise - “text/javascript” 11/15/2018 Copyright © Carl M. Burnett
14
How to Include JavaScript in a HTML Document
A script element in the head section that loads an external JavaScript file <script src="image_swap.js"></script> A script element that embeds JavaScript in the head section <head> ... <script> function printTOC() { window.print(); } </script> </head> Example 11/15/2018 Copyright © Carl M. Burnett
15
How to Include JavaScript in a HTML Document
A script element that embeds JavaScript in the body <p>© <script> var today = new Date(); document.writeln(today.getFullYear()); </script> </p> How to use the noscript tag <script> var today = new Date(); document.writeln(today.getFullYear()); </script> <noscript>2011</no 11/15/2018 Copyright © Carl M. Burnett
16
JavaScript Frameworks
Programming Library Includes: support programs, compilers, code libraries, tool sets, and application programming interfaces (APIs) inversion of control default behavior extensibility non-modifiable framework code jQuery Midori MooTools Webix AngularJS Comparisons of JavaScript Frameworks 11/15/2018 Copyright © Carl M. Burnett
17
Intro to jQuery jQuery Files you need for jQuery applications
The two jQuery libraries Files you need for jQuery applications jQuery (the core library) jQuery UI (User Interface) The jQuery JavaScript file The jQuery UI JavaScript file The jQuery UI stylesheet What jQuery offers Two ways to include the jQuery files Dozens of functions that make it easier to add JavaScript features to your web pages Functions that are tested for cross-browser compatibility Use a Content Delivery Network (CDN) like Google, Microsoft, or jQuery. Download and deploy on your web server. jQuery 11/15/2018 Copyright © Carl M. Burnett
18
How to include the jQuery files from a CDN
<!-- include the jQuery UI stylesheet --> <link rel="stylesheet" href=" 1.8.16/themes/base/jquery.ui.all.css"> <!-- include the jQuery and jQuery UI JavaScript files --> <script src=" 1.6.2/jquery.min.js"> </script> <script src=" 1.8.16/jquery-ui.js"> Note - The href and src attributes should be coded on a single line. 11/15/2018 Copyright © Carl M. Burnett
19
Including jQuery files on your Web Site
<!-- include the jQuery UI stylesheet --> <link rel="stylesheet" href="jquery.ui.all.css"> <!-- include the jQuery and jQuery UI JavaScript files --> <script src="jquery min.js"></script> <script src="jquery-ui.js"></script> 11/15/2018 Copyright © Carl M. Burnett
20
jQuery Syntax Basic syntax is: $(selector).action()
A $ sign to define/access jQuery A (selector) to "query (or find)" HTML elements A jQuery action() to be performed on the element(s) Examples: $(this).hide() - hides the current element. $("p").hide() - hides all <p> elements. $(".test").hide() - hides all elements with class="test". $("#test").hide() - hides the element with id="test".
21
jQuery Selectors Element Selector $("h1") #id Selector $("#accordion")
.class Selector $(".fadein") 11/15/2018 Copyright © Carl M. Burnett
22
Session 2 – Selector Exercises
23
jQuery Events Examples: moving a mouse over an element
selecting a radio button clicking on an element
24
jQuery Events Mouse Events Keyboard Events Form Events Document /
click dblclick mousecenter mouseleave Keyboard Events keypress keydown keyup Form Events submit change focus blur Document / Window Events load resize scroll unload
25
jQuery Syntax For Event Methods
$("p").click(); $("p").click(function(){ // action goes here!! });
26
jQuery Event Methods Click Event Double Click Event Focus Blur
Hover Event Mouse Down Event Mouse Enter Event Mouse Leave Event Mouse Up Event On Multiple Event
27
Session 2 – Event Exercises
28
jQuery Effects Method Description animate()
Runs a custom animation on the selected elements clearQueue() Removes all remaining queued functions from the selected elements delay() Sets a delay for all queued functions on the selected elements dequeue() Removes the next function from the queue, and then executes the function fadeIn() Fades in the selected elements fadeOut() Fades out the selected elements fadeTo() Fades in/out the selected elements to a given opacity fadeToggle() Toggles between the fadeIn() and fadeOut() methods
29
jQuery Effects Method Description finish()
Stops, removes and completes all queued animations for the selected elements hide() Hides the selected elements queue() Shows the queued functions on the selected elements show() Shows the selected elements slideDown() Slides-down (shows) the selected elements slideToggle() Toggles between the slideUp() and slideDown() methods slideUp() Slides-up (hides) the selected elements stop() Stops the currently running animation for the selected elements toggle() Toggles between the hide() and show() methods
30
Session 2 – Effects Exercises
Exercise 1 – Hide/Show Exercise 2 - Fade Exercise 3 - Slide Exercise 4 - Animate
31
jQuery HTML Method Description addClass()
Adds one or more class names to selected elements after() Inserts content after selected elements append() Inserts content at the end of selected elements appendTo() Inserts HTML elements at the end of selected elements attr() Sets or returns attributes/values of selected elements before() Inserts content before selected elements clone() Makes a copy of selected elements css() Sets or returns one or more style properties for selected elements detach() Removes selected elements (keeps data and events)
32
jQuery HTML Method Description empty()
Removes all child nodes and content from selected elements hasClass() Checks if any of the selected elements have a specified class name height() Sets or returns the height of selected elements html() Sets or returns the content of selected elements innerHeight() Returns the height of an element (includes padding, but not border) innerWidth() Returns the width of an element (includes padding, but not border) insertAfter() Inserts HTML elements after selected elements insertBefore() Inserts HTML elements before selected elements offset() Sets or returns the offset coordinates for selected elements (relative to the document)
33
jQuery HTML Method Description offsetParent()
Returns the first positioned parent element outerHeight() Returns the height of an element (includes padding and border) outerWidth() Returns the width of an element (includes padding and border) position() Returns the position (relative to the parent element) of an element prepend() Inserts content at the beginning of selected elements prependTo() Inserts HTML elements at the beginning of selected elements prop() Sets or returns properties/values of selected elements remove() Removes the selected elements (including data and events) removeAttr() Removes one or more attributes from selected elements
34
jQuery HTML Method Description removeClass()
Removes one or more classes from selected elements removeProp() Removes a property set by the prop() method replaceAll() Replaces selected elements with new HTML elements replaceWith() Replaces selected elements with new content scrollLeft() Sets or returns the horizontal scrollbar position of selected elements scrollTop() Sets or returns the vertical scrollbar position of selected elements text() Sets or returns the text content of selected elements toggleClass() Toggles between adding/removing one or more classes from selected elements unwrap() Removes the parent element of the selected elements
35
jQuery HTML Method Description val()
Sets or returns the value attribute of the selected elements (for form elements) width() Sets or returns the width of selected elements wrap() Wraps HTML element(s) around each selected element wrapAll() Wraps HTML element(s) around all selected elements wrapInner() Wraps HTML element(s) around the content of each selected element
36
Session 2 – HTML Method Exercises
Exercise 1 - Get Exercise 2 - Set Exercise 3 - Add Exercise 4 - Remove Exercise 5 - CSS Classes Exercise 6 - CSS Methods Exercise 7 - Dimensions
37
jQuery Traversing Method Description add()
Adds elements to the set of matched elements addBack() Adds the previous set of elements to the current set andSelf() Deprecated in version 1.8. An alias for addBack() children() Returns all direct children of the selected element closest() Returns the first ancestor of the selected element contents() Returns all direct children of the selected element (including text and comment nodes) each() Executes a function for each matched element end() Ends the most recent filtering operation in the current chain, and return the set of matched elements to its previous state
38
jQuery Traversing Method Description eq()
Returns an element with a specific index number of the selected elements filter() Reduce the set of matched elements to those that match the selector or pass the function's test find() Returns descendant elements of the selected element first() Returns the first element of the selected elements has() Returns all elements that have one or more elements inside of them is() Checks the set of matched elements against a selector/element/jQuery object, and return true if at least one of these elements matches the given arguments last() Returns the last element of the selected elements map() Passes each element in the matched set through a function, producing a new jQuery object containing the return values
39
jQuery Traversing Method Description next()
Returns the next sibling element of the selected element nextAll() Returns all next sibling elements of the selected element nextUntil() Returns all next sibling elements between two given arguments not() Remove elements from the set of matched elements offsetParent() Returns the first positioned parent element parent() Returns the direct parent element of the selected element parents() Returns all ancestor elements of the selected element parentsUntil() Returns all ancestor elements between two given arguments
40
jQuery Traversing Method Description prev()
Returns the previous sibling element of the selected element prevAll() Returns all previous sibling elements of the selected element prevUntil() Returns all previous sibling elements between two given arguments siblings() Returns all sibling elements of the selected element slice() Reduces the set of matched elements to a subset specified by a range of indices
41
Session 2 – Traversing Exercises
Exercise 1 - Ancestors Exercise 2 - Descendants Exercise 3 - Siblings Exercise 4 - Filtering
42
jQuery AJAX Method Description $.ajax() Performs an async AJAX request
$.ajaxPrefilter() Handle custom Ajax options or modify existing options before each request is sent and before they are processed by $.ajax() $.ajaxSetup() Sets the default values for future AJAX requests $.ajaxTransport() Creates an object that handles the actual transmission of Ajax data $.get() Loads data from a server using an AJAX HTTP GET request $.getJSON() Loads JSON-encoded data from a server using a HTTP GET request $.getScript() Loads (and executes) a JavaScript from a server using an AJAX HTTP GET request $.param() Creates a serialized representation of an array or object (can be used as URL query string for AJAX requests)
43
jQuery AJAX Method Description $.post()
Loads data from a server using an AJAX HTTP POST request ajaxComplete() Specifies a function to run when the AJAX request completes ajaxError() Specifies a function to run when the AJAX request completes with an error ajaxSend() Specifies a function to run before the AJAX request is sent ajaxStart() Specifies a function to run when the first AJAX request begins ajaxStop() Specifies a function to run when all AJAX requests have completed ajaxSuccess() Specifies a function to run when an AJAX request completes successfully load() Loads data from a server and puts the returned data into the selected element
44
jQuery AJAX Method Description serialize()
Encodes a set of form elements as a string for submission serializeArray() Encodes a set of form elements as an array of names and values
45
jQuery Miscellaneous Methods
Description data() Attaches data to, or gets data from, selected elements each() Execute a function for each matched element get() Get the DOM elements matched by the selector index() Search for a given element from among the matched elements $.noConflict() Release jQuery's control of the $ variable $.param() Create a serialized representation of an array or object (can be used as URL query string for AJAX requests) removeData() Removes a previously-stored piece of data size() Deprecated in version 1.8. Return the number of DOM elements matched by the jQuery selector toArray() Retrieve all the DOM elements contained in the jQuery set, as an array
46
Class Review jQuery Effects jQuery HTML jQuery Traversing jQuery AJAX
Intro to JavaScript Object Technology Document Object Model (DOM) JavaScript Frameworks Introduction to jQuery jQuery Syntax jQuery Selectors jQuery Events jQuery Effects jQuery HTML jQuery Traversing jQuery AJAX jQuery Misc Next – Session 3 Chapter 9 - Inspecting and Debugging your Website using Google Chrome Inspector 11/15/2018 Copyright © Carl M. Burnett
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.