1 Course Notes for WEB PROGRAMMING 4370/6370. 2 These notes are intended for use by studentsThese notes are intended for use by students These notes are.

Slides:



Advertisements
Similar presentations
JavaScript – Quiz #8 Lecture Code:
Advertisements

The jQuery library. What is jQuery ? A javascript lightweight library Is very easy to use Is powerful Is cross-browser compatible Downloadable from jQuery.com,
CT-376 jQuery Most popular javascript library today Latest version:
A really fairly simple guide to: mobile browser-based application development (part 1) Chris Greenhalgh G54UBI / Chris Greenhalgh
Modifying existing content Adding/Removing content on a page using jQuery.
JQuery A Javascript Library Hard things made eas(ier) Norman White.
Cascading Style Sheets CSS. What is it? Another file format ! its not like html Describes the looks of “selectors” in a.css file (example.css) in the.
Web Pages and Style Sheets Bert Wachsmuth. HTML versus XHTML XHTML is a stricter version of HTML: HTML + stricter rules = XHTML. XHTML Rule violations:
Automating Tasks With Macros
Chapter 16 Dynamic HTML and Animation The Web Warrior Guide to Web Design Technologies.
Philly.NET Hands-On jQuery + Plug-ins Bill Wolff, Rob Keiser.
1 Using jQuery JavaScript & jQuery the missing manual (Second Edition)
CST JavaScript Validating Form Data with JavaScript.
JQuery CS 268. What is jQuery? From their web site:
Bringing pages to life with jQuery BEAR BIBEAULT YEHUDA KATZ.
JavaScript & jQuery the missing manual Chapter 11
JQuery Page Slider. Our goal is to get to the functionality of the Panic Coda web site.Panic Coda web site.
Chapter 5 Java Script And Forms JavaScript, Third Edition.
CO1552 – Web Application Development Lists, Special Characters, and Tables.
A really fairly simple guide to: mobile browser-based application development (part 4, JQuery & DOM) Chris Greenhalgh G54UBI / Chris Greenhalgh.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
SYST Web Technologies SYST Web Technologies Lesson 6 – Intro to JavaScript.
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.
SERVER web page repository WEB PAGE instructions stores information and instructions BROWSER retrieves web page and follows instructions Server Web Server.
Adding User Interactivity – Lesson 51 Adding User Interactivity Lesson 5.
Review IDIA 619 Spring 2013 Bridget M. Blodgett. HTML A basic HTML document looks like this: Sample page Sample page This is a simple sample. HTML user.
JavaScript: Functions © by Pearson Education, Inc. All Rights Reserved.
Multi-Part Requests/ Parent & Child Service Items.
JavaScript, Fourth Edition
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
INTRODUCTION TO HTML5 Using jQuery with HTML5. Introducing jQuery  Although it is not a part of any W3C or WHATWG specification, jQuery performs an important.
Using Client-Side Scripts to Enhance Web Applications 1.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
1 Course Notes for CS1520 Programming Languages Part C By John C. Ramirez / Wonsun Ahn Department of Computer Science University of Pittsburgh.
Tutorial 6 Working with Web Forms. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Explore how Web forms interact with.
JavaScript, Fourth Edition
JQuery JavaScript is a powerful language but it is not always easy to work with. jQuery is a JavaScript library that helps with: – HTML document traversal.
Modifying HTML attributes and CSS values. Learning Objectives By the end of this lecture, you should be able to: – Select based on a class (as opposed.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Variables, Functions and Events (NON-Audio version) © Dr. David C. Gibbs WDMD.
Tutorial 6 Working with Web Forms. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Explore how Web forms interact with.
Intro to jQuery. What is jQuery? A JavaScript library Lightweight (about 31KB for the minified version) Simplifies HTML document traversing (DOM), event.
IS2802 Introduction to Multimedia Applications for Business Lecture 07: Introduction to jQuery Rob Gleasure
Unit 13 –JQuery Basics Instructor: Brent Presley.
Introduction to jQuery. 2 Objectives When you complete this chapter, you will be able to: Select elements using jQuery syntax Use built-in jQuery functions.
Changing HTML Attributes each() function Anonymous Functions $(this) keyword.
Positioning Objects with CSS and Tables
Introduction to JQuery COGS 187A – Fall JQuery jQuery is a JavaScript library, and allows us to manipulate HTML and CSS after the page has been.
© 2010 Delmar, Cengage Learning Chapter 11 Creating and Using Templates.
JQUERY AND AJAX
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
XP New Perspectives on Macromedia Dreamweaver MX 2004 Tutorial 5 1 Adding Shared Site Elements.
1 Using jQuery JavaScript & jQuery the missing manual (Second Edition)
Tarik Booker CS 120 California State University, Los Angeles.
JQuery is a fast, small, and feature-rich javascript library. It makes things like HTML document traversal and manipulation, event handling, animation,
Advanced HTML Tags:.
Introduction to.
Intro to jQuery jQuery is a popular, and extensible, JavaScript library Supports … DOM manipulation CSS manipulation HTML event methods Effects & animations.
Arrays and files BIS1523 – Lecture 15.
JQuery with ASP.NET.
Lecture 5: Functions and Parameters
An introduction to jQuery
An introduction to jQuery
An introduction to jQuery
Introduction to AJAX and JSON
JavaScript.
CNIT 133 Interactive Web Pags – JavaScript and AJAX
Web Programming and Design
Retrieving information from forms
Presentation transcript:

1 Course Notes for WEB PROGRAMMING 4370/6370

2 These notes are intended for use by studentsThese notes are intended for use by students These notes are provided free of charge and may not be sold in any shape or formThese notes are provided free of charge and may not be sold in any shape or form Material from these notes is obtained from various sources, including, but not limited to, the following:Material from these notes is obtained from various sources, including, but not limited to, the following:     Programming the World Wide Web, multiple editions, by Robert Sebesta (AW)

3 Lecture 1: Intro to JQuery What is JQuery?What is JQuery?  Not actually a language in and of itself  Rather, it is large library of Javascript code designed to make access and updates via DOM much simpler than with straight Javascript  Everything it does we could do without it, but it does make a lot of what we do easier You will use JQuery in Assignment 4You will use JQuery in Assignment 4

4 Lecture 1: JQuery Library How to include / access JQuery?How to include / access JQuery?  We can download the library locally and include the file Where localJQuery.js is our local copy of the libraryWhere localJQuery.js is our local copy of the library  Alternatively, we can link to a version on the Web: </script> Keeps code up to dateKeeps code up to date Must be online to use this, howeverMust be online to use this, however

Lecture 1: JQuery Initialization JQuery depends on the DOM being ready for accessJQuery depends on the DOM being ready for access  We do not want to use it until the page has been completely loaded  Once this has occurred we can use JQuery to access parts of our document (in various ways) and to manipulate them (also in various ways) 5

Lecture 1: JQuery Initializtion A good way to make sure the DOM is ready before using JQuery is to put our JQuery access statements into a callback function:A good way to make sure the DOM is ready before using JQuery is to put our JQuery access statements into a callback function: $(document).ready(function(){ // Rest of our JQuery code here will // execute when “ready” occurs }); }); Note the syntaxNote the syntax –Most JQuery commands are going to be prefixed by $ –This is (more or less) a function in the JQuery library that parses the rest of the data and invokes the appropriate function based on the command 6

Lecture 1: JQuery Initialization We will be able to do a LOT of things with this, all utilizing a fairly simple, consistent syntaxWe will be able to do a LOT of things with this, all utilizing a fairly simple, consistent syntax In the case of the ready() function, we are associating a function that contains the rest of our code with the ready eventIn the case of the ready() function, we are associating a function that contains the rest of our code with the ready event The ready event fires when the DOM is ready, and the function is calledThe ready event fires when the DOM is ready, and the function is called We can have a lot of code in the function body, including assignments of callbacks to events, etc.We can have a lot of code in the function body, including assignments of callbacks to events, etc. –We are setting up our document here, and waiting for events to occur 7

Lecture 1: Selection –Ex: Set an onclick callback to a button –Ex: Assign style to some text –Ex: Append text to an element In order to do the above we must be able to select elements / items in our documentIn order to do the above we must be able to select elements / items in our document  There are MANY ways of selection in JQuery  Let’s look at a few of them: Selecting by TAG name:Selecting by TAG name: $(“tagname”) –Returns an array of tags that match tagname Selecting by ID:Selecting by ID:$(“#theid”) –Returns element with id equal to theid 8

Lecture 1: Selection Selection by CSS class:Selection by CSS class:$(“.className”) –Returns an array of elements with class.className Selection by odd / even:Selection by odd / even: $(“element:odd”) –Returns array of items matching element with odd index values (with indices starting at 0) Selection by index:Selection by index: $(“element:eq(2)”) $(“element:lt(4)”) $(“element:gt(1)”) –Returns elements specified by index (eq = equal, lt = less than, gt = greater than) Plus MANY MANY MOREPlus MANY MANY MORE 9

Lecture 1: Selection We can even use selectors to find nested elements in a very intuitive wayWe can even use selectors to find nested elements in a very intuitive way $(“outerElement innerElement”) –This can be handy when we have several elements of the same type but we want to only modify nested elements within a certain one $(“element#id”) –This allows us to match a specific element with a specific id Let’s look at a simple exampleLet’s look at a simple example –See jqex1.html For more on selection see:For more on selection see: – – COOOOOOLNESS!!COOOOOOLNESS!! 10

Lecture 1: Modifying Elements  In the first example, we already saw how we can modify selected elements Basically, once an element has been selected we can do whatever we want to itBasically, once an element has been selected we can do whatever we want to it Some examples:Some examples:$(selector).css() »Update the CSS of the selected element(s) $(selector).append()$(selector).addClass()$(selector).attr()… »Many DOM methods to update properties of the element »See:

Lecture 1: Modifying Elements $(selector).hide()$(selector).show()… »Methods to change appearance of elements »See: $(selector).bind()$(selector).click()$(selector).focus()$(selector).mouseover()… »Methods to deal with events and event handling »See: We will look at some of these in more detailWe will look at some of these in more detail –Others you will need to look up at your leisure 12

Lecture 1: Modifying Elements  Note: Just as in most situations, there is often more than one way of doing things with JQuery Sometimes, one approach may be better than another, but in other situations they are just differentSometimes, one approach may be better than another, but in other situations they are just different Don’t assume the way I present something is the only way to do itDon’t assume the way I present something is the only way to do it –Or even necessarily the best way! 13

Lecture 1: Modifying Elements  Problem: We would like to iterate through the rows of a table and add a button to each row We want a click of the button to toggle a class assignment to the rowWe want a click of the button to toggle a class assignment to the row  Solution: 1)First we need to figure out how to iterate through the rows 2)We then must add a new button to each row 3)We must then add a click event handler to each new button 14

Lecture 1: Modifying Elements 1)JQuery has the each() iterator It iterates through each matched element in a selector, executing a callback function for eachIt iterates through each matched element in a selector, executing a callback function for each –The callback function receives two arguments, the current matched element and the current index (starting at 0) Note: The notion of iteration is common and we have seen it already in Java and PHPNote: The notion of iteration is common and we have seen it already in Java and PHP –The difference with this iterator is the way the code is executed – as a function call for each element We can use a selector to get the rows of the table and then use each() to access each one in turnWe can use a selector to get the rows of the table and then use each() to access each one in turn 15

Lecture 1: Modifying Elements 2)We can use the append() function This allows us to add arbitrary text / html to an elementThis allows us to add arbitrary text / html to an element We can add an input button to the current rowWe can add an input button to the current row 3)There are a couple of ways we can do this We can “hard code” the onclick attribute to a callback function that will toggle the classWe can “hard code” the onclick attribute to a callback function that will toggle the class We can access the button using JQuery immediately after adding it and use the click() function to assign the callback functionWe can access the button using JQuery immediately after adding it and use the click() function to assign the callback function  See jqex2.html and jqex2b.html 16

Lecture 2: Modifying Elements  Problem: We would like the font size of our document to automatically adjust as we increase or decrease the window width  Solution: 1)We need to detect the width of the document 2)We need to calculate a font size based on that width 3)We need to recognize a resize event and call a function to update the font size 17

Lecture 2: Modifying Elements 1)We can use the width() function to find out the width of the document 2)There are several ways ways that we can do this Depends on how we are actually storing / keeping the fontDepends on how we are actually storing / keeping the font CSS allows for many different font metricsCSS allows for many different font metrics –pt (points) –px (pixels) –em (ems) –% (percent of the default size for the browser) »For scaling it is probably better to use em or % »If you need a fixed size, you can use pt or px 18

Lecture 2: Modifying Elements See:See: – – em-vs-px-vs-pt-vs/ em-vs-px-vs-pt-vs/ em-vs-px-vs-pt-vs/ In any case, we can calculate a new font size based on the relative width of the resized window vs. that of the originalIn any case, we can calculate a new font size based on the relative width of the resized window vs. that of the original 3)JQuery has a resize() function that takes a callback for the resize event We simply put code into this function that we want to execute each time the window is resizedWe simply put code into this function that we want to execute each time the window is resized  See jqex3.html and jqex3b.html 19

Lecture 2: Modifying Elements Problem: We want to show the user a list of items and have him/her rank them by the order they are chosenProblem: We want to show the user a list of items and have him/her rank them by the order they are chosen  They will then be placed into a new list showing the ranking Solution:Solution: 1)Iterate through list, attaching on click event handler 2)Handler will remove list item from an unordered list and add it to an ordered list 20

Lecture 2: Modifying Elements 1)We can again use the each() function to iterate through all elements in an unordered list To each we assign a callback function for the click() eventTo each we assign a callback function for the click() event For a bit of added style we also use the hover() function to update the style when the mouse is over the elementFor a bit of added style we also use the hover() function to update the style when the mouse is over the element 2)For the current element, we simply remove() it from the unordered list and append it to an ordered list ( ) 21

Lecture 2: Modifying Elements We also handle some special cases:We also handle some special cases: –Initially there is NO ordered list, so for the first element selected we must create the –Initially there is NO ordered list, so for the first element selected we must create the –After all items have been moved, the old is empty, so we remove that  See jqex4.html  Yes, this is some neat stuff! 22

Lecture 2: AJAX JQuery has a lot of AJAX convenience functionsJQuery has a lot of AJAX convenience functions See:  They allow the full AJAX capabilities As if you were using XMLHttpRequest directlyAs if you were using XMLHttpRequest directly  They also allow some “shorthand” access  Let’s look at a few functions $.ajax()$.ajax() –Most general ajax() function –Has options to more or less do whatever you want »Headers, callbacks, cache, etc 23

Lecture 2: AJAX  However, in many cases we use a specific variation of an AJAX call  JQuery has some predefined variations that are very convenient.load().load() –This is actually a function that is called from a matched / selected element –It makes an AJAX call to update the selected element’s data from a server –Ex: $(“#theTable tr”).load(‘url’); »Will get the file from ‘url’ (which could be the result of a script) and load it into the selected table row »We can also put in a callback function but it is not required 24

Lecture 2: AJAX.get(),.post().get(),.post() –Make AJAX requests with the stated default way of passing parameters –Both can supply options to the server Many other functions are available to specific optionsMany other functions are available to specific options  AJAX return data: Cool thing in JQuery:Cool thing in JQuery: –Default is Intelligent Guess »Not a scientific theory! »JQuery looks at the header / format of the returned document and makes its judgment about the type »Automatically parses it based on that guess 25

Lecture 2: AJAX »If the data is JSON it creates a Javascript object from it (we will discuss JSON shortly) »If the data is XML it creates an XML document tree that can be parsed using JQuery selectors –Either way, the document can be nicely accessed once retrieved –Let’s look at a few examples: 26