• No results found

Lec 08 JQuery & Ajax.pptx

N/A
N/A
Protected

Academic year: 2020

Share "Lec 08 JQuery & Ajax.pptx"

Copied!
39
0
0

Loading.... (view fulltext now)

Full text

(1)

1

CS428 Web Engineering

Lecture 08

JavaScript Library

(2)

Today’s Goal:

Jquery & Ajax

To become able to appreciate the concept of

JavaScript libraries including JQuery:

What are they?

What do they do?

How do we benefit from them?

(3)

3

JavaScript Libraries(1)

JavaScript is really a small language.

• But unlike technologies like C++, Java, .Net etc

it doesn’t have dozens of built-in libraries with thousands of objects to use.

A lot of people write code in JavaScript and

bundled it together into one of the form of

(4)

JavaScript Libraries(2)

JavaScript libraries simply means the

JavaScript code that someone else wrote and you don’t have to.

• So, what are these libraries, they are dozens of

them, we just used few

• Some of these libraries like google closure,

(5)
(6)
(7)
(8)
(9)
(10)

JavaScript Libraries(3)

They have whole lots of function that helps navigating to the DOM, they include code for

cross browser detection. Easier to work with

events without worrying which version of

Internet Explorer you might using or working with animations.

• Make it easy to have your JavaScript request

(11)

11

To use any of these libraries I just have to go

there and downloaded.

For Example: www.jquery.com

• You just download it and link it like you link your

(12)

Now on other hand some JavaScript libraries

are more specialized rather then the general purpose.

• Example: www.huddletogether.com LightBox

are simple JavaScript libraries for popping up images on your website.

They might be use for implementing things like

(13)
(14)
(15)

15

They help you to add curve corners etc

Most of these libraries are open source and they

are free, No licensing

You simply grab the code and use it.

• The most general purpose JavaScript library is

JQuery. Knowing the JavaScript library particularly JQuery is core skill for a JavaScript developer

(16)

What is JQuery?

JQuery is a lightweight,”write less, do more”

JavaScript library.

The purpose of jQuery is to make it much

easier to use JavaScript on your website.

• How to use it?

• Visit www.jquery.com and download both

(17)

Downloading JQuery

There are two versions of jQuery available for

downloading:

Production version - this is for your live website

because it has been minified and compressed

Development version - this is for testing and

development (uncompressed and readable code)

• Both versions can be downloaded from

jQuery.com.

(18)
(19)

jQuery Environment Setup

<!DOCTYPE html>

<html lang=‘en’> <head>

<title>Title of page</title>

<script type="text/javascript"

src="/js/jquery-2.1.3.min.js"></script>

</head>

<body>

This is my first homepage. <b>This text is bold</b>

</body>

</html>

(20)

jQuery CDN

If you don't want to download and host jQuery yourself, you can include it

from a CDN (Content Delivery Network).

Both Google and Microsoft host jQuery.

Google CDN:

<head>

<script

src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></ script>

</head>

Microsoft CDN:

<head>

(21)

21

THE DOCUMENT READY EVENT

$(document).ready(function(){ // jQuery methods go here...

});

This is to prevent any jQuery code from running before

the document is finished loading (is ready).

It is good practice to wait for the document to be fully

(22)

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

(23)

23

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

(24)

JQUERY SELECTORS

jQuery selectors allow you to select and

manipulate HTML element(s).

With jQuery selectors you can find elements

based on their id, classes, types, attributes,

values of attributes and much more. It's based

on the existing CSS Selectors, and in addition, it has some own custom selectors.

(25)

25

THE ELEMENT SELECTOR

The jQuery element selector selects elements

based on their tag names. You can select all <p> elements on a page like this:

• Example:

(26)

26

THE #ID SELECTOR

The jQuery #id selector uses the id attribute of

an HTML tag to find the specific element.

To find an element with a specific id, write a

hash character, followed by the id of the element:

• Example:

(27)

27

THE

.

CLASS SELECTOR

The jQuery class selector finds elements with a

specific class.

To find elements with a specific class, write a

period character, followed by the name of the class:

• Example:

(28)

28

MORE EXAMPLES OF SELECTOR

Syntax Description

• $("*") Selects all elements

• $(this) Selects the current HTML

element

• $("p.intro") Selects all <p> elements with

class="intro"

$("p:first") Selects the first <p> element$("[href]") Selects all elements with an

href attribute

(29)

REGULAR JavaScript vs. JQuery

Without JQuery we often write lines of code like this:

document.getElementById(“myDiv”).className = “highlight”;

jQuery(“#myDiv”).addClass(“highlight”);

there is two reason why JQuery code is preferable:

1) getElementById(“myDiv”) we must have an ID “myDiv” in HTML, if we doesn’t have ID “myDiv” we can’t get to it. But JQuery is much more flexible

2) We can give JQuery ID to find using hash sign, but we can also find other parts of the page that we can’t get

element by ID. For Example: we can get all the elements belong to a particular class

jQuery(“.someClass”)

(30)

jQuery(“p”) jQuery(“a”)

jQuery(“li”)

• jQuery(“#myDiv”).addClass(“highlight”);

We are telling jQuery what we wanted to find

• jQuery(“p.description”)

• Give me all the paragraph that have a class

(31)

Further words we can add to further refine

our jQuery statement.

:first :last

:contains() :visible

(32)

JQUERY METHODS

$( “what to find” ).someAction;

• $( “#myDiv” ).addClass( “highlight” );

.removeClass( “highlight” ); .toggleClass( “highlight” );

Note:

(33)

JQUERY ALIAS

jQuery( “#myDiv” ).addClass( “highlight” );

• $( “#myDiv” ).addClass( “highlight” );

General Format:

$( “what to find” ).someAction(any params);

(34)

What is AJAX?

AJAX stands for Asynchronous JavaScript And

XML

Allows web pages or parts of them to be

updated asynchronously

Based on XML HTTP request object aka ( XHR)

Requires basic understanding of:

(X)HTML – displaying the data/Dynamic content

CSS – styling the data

JavaScript – manipulating the DOM

(35)

Ajax -

Usage samples

Autocomplete search textboxes ( sample)

Cascading dropdown list boxes (sample )

Real-time - Continuous data refresh (long polling, chat systems…)

Immediate forms validation feedback ( sample)

Conditional display / dynamic content ( sample)

Auto save user information (Google Docs, Facebook)

Ratings, voting & other instant actions (sample)

(36)
(37)
(38)

Advantage of Ajax

Better interactivity & responsiveness

Impressive UX

Reduced connections to the web server

(partial rendering)

(39)

Disadvantage of Ajax

The back and refresh button are rendered useless

Bookmarking is useless (HTML 5 History API to the rescue)

Requires JavaScript to be enabled on browsers

SEO & analytics unfriendly

Screen readers unfriendly – affects accessibility

Callbacks can lead to complex code

Network latency may break usability

References

Related documents

Against this backdrop, in an attempt to consider the differential washback effects of task based language assessment procedure and traditional assessment modes on the follow up

Not surprisingly, film festival research is conducted in the humanities as well as social sciences, most notably by film and media scholars but also as part

Results of analysis of variance of 10 characters for 3 improved Irish potato and one local check of the respective location are presented in (Table 1) Plant height, average

The property that the detail equals zero over constant regions is a natural requirement which causes the SHAH algorithm to offer sparse representations for piecewise-constant images,

Define the calibration measurement parameter values, ranges, uncertainty limits, confidence levels, and recalibration time limits (calibration intervals) that match measurement

My staff conducted a survey of insurance agents and brokers serving businesses located throughout the five boroughs and asked them to report rate increases and availability for

Our BVR framework encourages clients to build the connect from the process metrics to the business metrics to achieve their desired results in business metrics such as reduction

In a nutshell, low-powered contracts emerge in equilibrium because a buyer, when designing a procurement contract to an activity provider, does not fully internalize that