1 Using jQuery JavaScript & jQuery the missing manual (Second Edition)

Slides:



Advertisements
Similar presentations
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,
Advertisements

Chapter 3 – Designing your web pages Dr. Stephanos Mavromoustakos.
11 Getting Started with ASP.NET Beginning ASP.NET 4.0 in C# 2010 Chapters 5 and 6.
The Web Warrior Guide to Web Design Technologies
Web Development in Microsoft Visual Studio Slide 2 Lecture Overview Introduce Visual Studio 2013 Create a first ASP.NET application.
Chapter 16 Dynamic HTML and Animation The Web Warrior Guide to Web Design Technologies.
Chapter 1 Getting Started With Dreamweaver. Explore the Dreamweaver Workspace The Dreamweaver workspace is where you can find all the tools to create.
Philly.NET Hands-On jQuery + Plug-ins Bill Wolff, Rob Keiser.
1 Using jQuery JavaScript & jQuery the missing manual (Second Edition)
Definition from Wikipedia.  The Prototype JavaScript Framework  implemented as a single file of JavaScript code  named prototype.js (
 Using Microsoft Expression Web you can: › Create Web pages and Web sites › Set what you site will look like as you design it › Add text, images, multimedia.
DHTML. What is DHTML?  DHTML is the combination of several built-in browser features in fourth generation browsers that enable a web page to be more.
Tutorial: Introduction to ASP.NET Internet Technologies and Web Application 4 th February 2010.
PhpXperts What is jQuery Javascript Library Fast and concise Simplifies the interaction between HTML and JavaScript.
JavaScript & jQuery the missing manual Chapter 11
1 The Document Object Model SAMS Teach Yourself JavaScript in 24 Hours (Fourth Edition) Michael Moncur SAMS Publishing 2007.
JQuery 10/21. Today jQuery Some cool tools around the web JavaScript Libraries Drawing libraries HTML Frameworks Conventions.
A really fairly simple guide to: mobile browser-based application development (part 4, JQuery & DOM) Chris Greenhalgh G54UBI / Chris Greenhalgh.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
JQUERY | INTRODUCTION. jQuery  Open source JavaScript library  Simplifies the interactions between  HTML document, or the Document Object Model (DOM),
SEG3210 DHTML Tutorial. DHTML DHTML is a combination of technologies used to create dynamic and interactive Web sites. –HTML - For creating text and image.
13. jQuery See the official documentation at  See the terse API documentation at
CITS1231 Web Technologies JavaScript and Document Object Model.
1 Data Bound Controls II Chapter Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available.
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.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
Animation & Effects Using JQuery. What is jQuery? jQuery is a lightweight, JavaScript library. The purpose of jQuery is to make it much easier to use.
Use CSS to Implement a Reusable Design Selecting a Dreamweaver CSS Starter Layout is the easiest way to create a page with a CSS layout You can access.
1 JavaScript Functions and Objects. 2 Objectives You will be able to Use JavaScript functions in JavaScript scripts. Get JavaScript functions executed.
JQuery Introduction © Copyright 2014, Fred McClurg All Rights Reserved.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.2 Revised by Dr. T. Tran for CSI3140.
JavaScript Library. What is jQuery jQuery is a lightweight JavaScript library. The purpose is to make it easier to use JavaScript code on your website.
1 Javascript DOM Peter Atkinson. 2 Objectives Understand the nature and structure of the DOM Add and remove content from the page Access and change element.
Review of the DOM Node properties and methods Some ways of accessing nodes Appending, copying and removing nodes Event handling – Inline – Scripting –
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.
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.
Introduction to Programming the WWW I CMSC Summer 2003 Lecture 9.
Understanding JavaScript and Coding Essentials Lesson 8.
11 Getting Started with ASP.NET Beginning ASP.NET in C# and VB Chapters 1 and 2.
Dr. Ahmet Cengizhan Dirican BIL 374 Internet Technologies 6. Dynamic Documents With JavaScript.
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.
1111 Master Pages Beginning ASP.NET in C# and VB Chapter 6.
11 User Controls Beginning ASP.NET in C# and VB Chapter 8.
Chapter 10 Dynamic HTML (DHTML) JavaScript, Third Edition.
JQUERY AND AJAX
Microsoft Expression Web - Illustrated Unit A: Getting Started With Microsoft Expression Web.
CSCI 3100 Tutorial 5 JavaScript & Ajax Jichuan Zeng Department of Computer Science and Engineering The Chinese University of Hong.
111 State Management Beginning ASP.NET in C# and VB Chapter 4 Pages
1 Project 8: Tic Tac Toe. 2 Assignment Write an HTML and JavaScript page that pemits users to play Tic Tac Toe. Both players use a single browser. Alternating.
1 The Document Object Model. 2 Objectives You will be able to: Describe the structure of the W3C Document Object Model, or DOM. Use JavaScript to access.
What is jQuery?.
DHTML.
ASP.NET Forms.
Programming Web Pages with JavaScript
ASP.NET Web Controls.
Using DHTML to Enhance Web Pages
Tek Raj Chhetri Code for Humans not for machine.
Human Computer Interaction
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
Revision.
Web Development in Microsoft Visual Studio 2013
jQuery The Easy JavaScript Nikolay Chochev Technical Trainer
JQuery with ASP.NET.
Introduction to Programming the WWW I
© 2015, Mike Murach & Associates, Inc.
Introduction to Programming and JavaScript
Presentation transcript:

1 Using jQuery JavaScript & jQuery the missing manual (Second Edition)

2 Objectives You will be able to Describe what jQuery is and what it does. Use jQuery to hide browser dependencies in JavaScript scripts. Use jQuery in JavaScript to access and modify the DOM.

3 jQuery A JavaScript framework Extensive JavaScript script that provides a more abstract DOM API for client scripts. Hides browser differences. API works for all widely used browsers. Open Source Developed and maintained by volunteers Has emerged as the preeminent JavaScript framework. Supported by Microsoft

4 jQuery jQuery leverages the CSS selection syntax to identify DOM elements. Identify DOM elements for JavaScript functions the same way we identify them to apply CSS styles. Most web developers are familiar with CSS. More powerful and more concise API.

5 Adding jQuery to an App You can either Download jQuery and put a copy into your web site directory. OR Reference one of several Content Distribution Networks that support jQuery. Server downloads jQuery from the CDN at run time.

6 The Microsoft CDN

Scroll Down 7

8 Using jQuery As a demonstration of jQuery, we will modify the DOM positioning script from last week to use jQuery rather than the DOM API. Anything you can do one way you can do the other. But jQuery is often more convenient.

9 Using jQuery Create a new empty ASPX website called DOM_Positioning_jQuery Download to desktop final version of DOM_Positioning website from last week: Downloads/2016_03_31_DOM_Positioning/ Downloads/2016_03_31_DOM_Positioning/ File DOM_Positioning.zip Expand the.zip file

10 Using jQuery In Visual Studio, add existing item: Select all five files.

Initial Website 11 Set positioning.html as start page. Build and run.

12 Initial Website

13 Download jQuery

14 Download jQuery Download current version minified to website folder. Add to website

Add Existing Item 15

jQuery in Solution 16

17 Add jQuery to the Page Add tag to head as first script immediately after the title. <script language="javascript" type="text/javascript" src="jquery min.js" > The jquery file must be loaded before animate.js Update the title Positioning Elements with jQuery

18 positioning.html

19 Using jQuery We will modify the original JavaScript scripts to use jQuery In small steps. Understand each step! Build and test after each step.

20 animate.js Modify to use jQuery.

21 The jQuery $( ) $('#square') returns a jQuery object An array of JavaScript DOM node objects All nodes with ID square. There is only one. $('#square')[0] returns a reference to the first (and only) element of the array. In this case, a DOM element. Effect is the same as for sq = document.getElementById("square");

22 The jQuery $( ) $( ) is a JavaScript function call Shorthand for jQuery( ) A function defined in jQuery.js Everything we do with jQuery will have this form. The function returns a jQuery object. The object has properties and methods that we can use. $( ) takes a parameter that is identical to a CSS selector. If you know CSS, you already have a good start toward understanding jQuery!

23 Try it! Click these buttons to test our change.

24 Replace All getElementById("square")

25 Adding Content with jQuery The jQuery append() method adds its parameter value as the last child element of the selected element. Let's use this jQuery method in function modifySquareText. Comment out entire function and replace with this: function modifySquareText() { var new_text = "Blah, blah, blah "; $('#square').append(new_text); } Try it!

26 Adding Content with jQuery Click Change Text

27 Modified Square

28 Getting User Input in jQuery Select text input element. Use the jQuery val() method to get the input. Let's modify function modifySquareText to use jQuery val() to get user input from a textbox and add it to the text in the square. Add a textbox below the buttons. id = input_box

Add a Textbox 29

animate.js function modifySquareText() { $('#square').append($('#input_box').val()); // Clear the input box. $('#input_box').val(''); } 30 val without an argument gets the value val with an argument sets the value

App with Textbox 31 Enter some text and then click “Change Text”

User Input 32

33 After Change Text Clicked End of Section

34 Let the User Drag and Drop the Square We can handle MouseDown, MouseMove, and MouseUp events in order to implement drag and drop in JavaScript. Note: This is a simple example. In a real app we would typically do something in response to the drag and drop rather than just moving the object on the screen.

35 Drag and Drop Add a new JavaScript file to the website: drag_and_drop.js

36 Drag and Drop New tag in positioning.html head: <script language="javascript" type="text/javascript" src="drag_and_drop.js"> After jquery and before animate

37 drag_and_drop.js var dx, dy; // Set up when the page loads window.onload = Setup; function Setup() { $('#square').bind('mousedown', Start_Drag); } Note alternative to Avoid cluttering the HTML!

38 Drag and Drop in Progress Apps typically provide feedback to the user indicating that a drag and drop operation is in progress. Let's make the border red while the square is being dragged.

39 css() The jQuery css() method sets style attributes for jQuery objects. To set the border of the square red we can write: var sq = $('#square'); sq.css('borderColor', 'Red');

40 function Start_Drag() The argument passed to the event handler Start_Drag is a jQuery object. Its methods are the same for all browsers supported by jQuery. Eliminates the need for browser dependent code.

41 function Start_Drag function Start_Drag(e) { var mouse_x = e.pageX; var mouse_y = e.pageY; var sq = $('#square'); sq.css('borderColor', 'Red'); // Calculate x and y distances from mouse position // to upper left corner of the square. dx = mouse_x - sq[0].offsetLeft; dy = mouse_y - sq[0].offsetTop; // Set event handlers sq.bind('mousemove', Move); sq.bind('mouseup', Drop); sq.unbind('mousedown'); }

function Move // Track mouse movements function Move(e) { var new_mouse_x = e.pageX; var new_mouse_y = e.pageY; // Update position of the square var new_x = new_mouse_x - dx; var new_y = new_mouse_y - dy; var sq = $('#square'); sq.css('left', new_x); sq.css('top', new_y); } 42

43 function Drop function Drop() { var sq = $('#square'); sq.css('borderColor', 'black'); sq.unbind('mousemove'); sq.unbind('mouseup'); sq.bind('mousedown', Start_Drag); } Try it!

44 Internet Explorer 10

45 Cultural Issues The jQuery culture encourages the use of anonymous functions. Keep the namespace sparse. Avoid potential name conflicts. If a function is only used in one place use the function definition rather than a function name.

46 An Anonymous Function Try it! (It works the same.)

47 Chaining Many jQuery functions return a reference to a jQuery object. Can be used to perform another operation without doing a new selection.

48 Chaining css() At the end of Move() Instead of var sq = $('#square'); sq.css('left', new_x); sq.css('top', new_y); we can write $('#square').css('left', new_x).css('top', new_y);

49 Chaining bind() At the end of Start_Drag() $('#square').bind('mousemove', Move).bind('mouseup', Drop).unbind('mousedown'); At the end of Drop() $('#square').unbind('mousemove').unbind('mouseup').bind('mousedown', Start_Drag);

50 Different Browsers jQuery generally does a good job of hiding browser differences. BUT Always check your app in multiple browsers.

51 Chrome

52 Firefox

53 Safari

54 Summary jQuery makes DOM manipulations easier by hiding browser differences. More concise syntax than the DOM API. Leverages knowledge of CSS selectors.

55 References Documentation for the jQuery methods that we have used can be found in JavaScript & jQuery the missing manual and on the jQuery web site. append page bind page css page val page