Advanced File Processing

Download Report

Transcript Advanced File Processing

JQuery
Web Technology
Derived from:
http://www.w3schools.com/jquery/
What We Should Already Know
• Before we start studying jQuery, we should have
a basic knowledge of:
– HTML
– Cascading Style Sheet (CSS)
– Javascript
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 our website.
• jQuery also simplifies a lot of the complicated
things from JavaScript, like AJAX calls and DOM
manipulation.
JQuery Library
• The jQuery library contains the following
features:
– HTML/DOM manipulation
– CSS manipulation
– HTML event methods
– Effects and animations
– AJAX
– Utilities
Using jQuery Library
• jQuery library is a single JavaScript file, and we can
reference it with the HTML <script> tag
– Notice that: the <script> tag should be inside the <head> section
<head>
<script src="jquery-1.9.1.min.js"></script>
</head>
• Note: we can download jQuery library from jquery.com
Using Online jQuery Library
• If we don't want to download and host jQuery, we can
include it from a CDN (Content Delivery Network).
– To use jQuery from Google, use the following:
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
</head>
– To use jQuery from Microsoft CDN, use:
<head>
<script src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.1.min.js">
</script>
</head>
JQuery Syntax
• jQuery syntax is tailor made for selecting HTML
elements and performing some action on the
element(s).
• Basic syntax is: $(selector).action()
– $ sign is to define/access jQuery
– selector is to "query (or find)" HTML elements
– action() is function to be performed on the element(s)
Examples:
$(this).hide()
$("p").hide()
$(".test").hide()
$("#test").hide()
- hides the current element.
- hides all <p> elements.
- hides all elements with class="test".
- hides the element with id="test".
The Document Ready Event
• jQuery methods are inside a 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).
• We can use a shorter method for the document ready
event as follow:
$(function(){
// jQuery methods go here...
});
Element Selector
• The jQuery element selector selects elements based on
their tag names.
• We can select all <p> elements on a page like this:
$("p")
• Example:
– When a user clicks on a button, all <p> elements will be hidden:
$(document).ready(function(){
$("button").click(function(){
$("p").hide();
});
});
The #id Selector
• We should use #id selector when we want to find a
single, unique element in the page.
• To find an element with a specific id, write a hash
character, followed by the id of the element:
$("#test")
• Example:
– When a user clicks on a button, the element with id="test" will be
hidden:
$(document).ready(function() {
$("button").click(function() {
$("#test").hide();
});
});
The .class Selector
• To find elements with a specific class, write a period
character, followed by the name of the class:
$(".test")
• Example:
– When a user clicks on a button, the elements with class="test"
will be hidden:
$(document).ready(function(){
$("button").click(function(){
$(".test").hide();
});
});
More Examples of jQuery Selectors
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
$("ul li:first")
Selects the first <li> element of the first <ul>
$("ul li:first-child")
Selects the first <li> element of every <ul>
$("[href]")
Selects all elements with an href attribute
$("a[target='_blank']")
Selects all <a> elements with a target attribute value equal to
"_blank"
$("a[target!='_blank']") Selects all <a> elements with a target attribute value NOT equal
to "_blank"
$(":button")
Selects all <button> elements and <input> elements of
type="button"
$("tr:even")
Selects all even <tr> elements
$("tr:odd")
Selects all odd <tr> elements
Functions in a Separate File
• If our website contains a lot of pages, and we want our
jQuery functions to be easy to maintain,
– we can put our jQuery functions in a separate .js file.
• In addition, our code would look nicer and more tidy.
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script src="my_jquery_functions.js"></script>
</head>
JQuery Events
• All the different visitor's actions that a web page can
respond to are called events
• An event represents the precise moment when
something happens.
• Examples:
– moving a mouse over an element
– selecting a radio button
– clicking on an element
The term "fires" is often used with events.
Example: "The keypress event fires the moment we press a key".
DOM Events
• The followings are some example DOM events:
Mouse Events Keyboard Events Form Events
Document/Window
Events
click
keypress
submit
load
dblclick
keydown
change
resize
mouseenter
keyup
focus
scroll
blur
unload
mouseleave
click()
• The function is executed when the user clicks on the
HTML element.
• The following example says:
– When a click event fires on a <p> element; hide the current <p>
element:
$("p").click(
function() {
$(this).hide();
}
);
dbclick()
• dbclick() method attaches an event handler function to
an HTML element.
• The function is executed when the user double-clicks on
the HTML element:
$("p").dblclick(
function() {
$(this).hide();
}
);
mouseenter()
• mouseenter() method attaches an event handler function
to an HTML element.
• The function is executed when the mouse pointer enters
the HTML element:
$("#p1").mouseenter(
function() {
alert("You entered p1!");
}
);
Other Mouse Events
• mouseleave()
– The mouseleave() method attaches an event handler function to
an HTML element.
• mousedown()
– The mousedown() method attaches an event handler function to
an HTML element.
• mouseup()
– The mouseup() method attaches an event handler function to an
HTML element.
hover()
• hover() method takes two functions and is a combination
of the mouseenter() and mouseleave() methods.
• The first function is executed when the mouse enters the
HTML element,
• The second function is executed when the mouse leaves
the HTML element:
$("#p1").hover(
function() {
alert("You entered p1!");
},
function(){
alert("Bye! You now leave p1!");
}
);
focus()
• focus() method attaches an event handler function to an
HTML form field.
• The function is executed when the form field gets focus:
$("input").focus(
function() {
$(this).css("background-color","#cccccc");
}
);
blur()
• blur() method attaches an event handler function to an
HTML form field.
• The function is executed when the form field loses focus:
$("input").blur(
function() {
$(this).css("background-color","#ffffff");
});
jQuery hide() and show()
• Syntax:
$(selector).hide(speed, callback);
$(selector).show(speed, callback);
– Optional speed parameter can take the following values:
"slow", "fast", or milliseconds.
– Optional callback parameter is a function to be executed after
the hide() or show() method completes.
• With jQuery, we can hide and show HTML elements with
the hide() and show() methods as examples below:
$("#hide").click(function() {
$("p").hide();
});
$("#show").click(function() {
$("p").show();
});
jQuery toggle()
• Syntax:
$(selector).toggle(speed,callback);
– Optional speed values can be: "slow", "fast", or milliseconds.
– Optional callback function is a function to be executed after
toggle() completes.
• We can toggle between hide() and show() methods with
toggle() method.
• Shown elements are hidden and hidden elements are
shown:
$("button").click(
function() {
$("p").toggle();
}
);
The animate() Method
• animate() method is used to create custom animations.
$(selector).animate( {params}, speed, callback);
– Required params defines CSS properties to be animated.
– Optional speed specifies duration of the effect. It can take the
following values: "slow", "fast", or milliseconds.
– Optional callback is a function to be executed after the animation
completes.
Example of animate() Method
• The following example demonstrates a simple use of the
animate() method;
– it moves a <div> element to the left, until it has reached a left
property of 250px:
$("button").click( function() {
$("div").animate({left:'250px'});
});
• This example uses multiple properties:
$("button").click(function() {
$("div").animate( {
left:'250px',
opacity:'0.5',
height:'150px',
width:'150px'
});
});
animate() - Using Relative Values
• It is also possible to define relative values
– The value is then relative to the element's current value.
• This is done by putting += or -= in front of the value:
$("button").click(
function() {
$("div").animate( {
left:'250px',
height:'+=150px',
width:'+=150px'
});
}
);
animate() - Using Pre-defined Values
• We can even specify a property's animation value as
"show", "hide", or "toggle":
$("button").click(
function() {
$("div").animate( {
height:'toggle'
});
}
);
animate() - Uses Queue Functionality
• We can write multiple animate() calls after each other.
• jQuery creates an "internal" queue with these method
calls.
• If we want to perform different animations after each
other, we take advantage of the queue functionality:
$("button").click(function() {
var div = $("div");
div.animate({height:'300px',opacity:'0.4'}, "slow");
div.animate({width:'300px',opacity:'0.8'}, "slow");
div.animate({height:'100px',opacity:'0.4'}, "slow");
div.animate({width:'100px',opacity:'0.8'}, "slow");
});
Example of Queue Functionality
• This example first moves the <div> element to the right,
and then increases the font size of the text:
$("button").click(
function() {
var div=$("div");
div.animate( { left:'100px‘ }, "slow");
div.animate( { fontSize:'3em‘ }, "slow");
}
);
stop() Method
$(selector).stop( stopAll, goToEnd);
• Optional stopAll specifies whether also the animation
queue should be cleared or not.
– Default is false, which means that only active animation will be
stopped, allowing any queued animations to be performed
afterwards.
• Optional goToEnd specifies whether or not to complete
the current animation immediately.
– Default is false.
• So, by default, the stop() method kills current animation
being performed on the selected element.
Example of stop() Method
• Following example demonstrates the stop() method, with
no parameters:
$("#stop").click(
function() {
$("#panel").stop();
}
);
jQuery Callback Functions
• JavaScript statements are executed line by line.
• However, with effects, the next line of code can be run
even though the effect is not finished.
– This can create errors.
• To prevent this, we can create a callback function.
• A callback function is executed after the current effect is
finished.
• A typical syntax:
$(selector).hide(speed, callback);
Example of Callback Functions
• Example with callback
$("button").click(function(){
$("p").hide("slow", function(){
alert("The paragraph is now hidden");
});
});
• Example without callback
$("button").click(function(){
$("p").hide(1000);
alert("The paragraph is now hidden");
});
Method Chaining
• Chaining allows us to run multiple jQuery commands,
one after the other, on the same element(s).
• To chain an action, we simply append action to the
previous one.
• Following example chains together the css(), slideUp(),
and slideDown() methods.
– The "p1" element first changes to red, then it slides up, and then
it slides down:
$("#p1").css("color", "red").slideUp(2000).slideDown(2000);
Tip on Method Chaining
• When chaining, line of code could become quite long.
• However, jQuery is not very strict on the syntax;
– we can format it like we want, including line breaks and
indentations.
• So, this also works just fine:
$("#p1").css("color", "red")
.slideUp(2000)
.slideDown(2000);
• Note:
– jQuery throws away extra whitespace and executes the lines
above as one long line of code.
Get/Set Content and Attributes
• jQuery contains powerful methods for changing and
manipulating HTML elements and attributes.
• Three simple useful jQuery methods for DOM
manipulation are:
– text() - sets or returns the text content of selected elements
– html() - sets or returns the content of selected elements
(including HTML markup)
– val() - sets or returns the value of form fields
Get Content and Value
• The following example demonstrates how to get content
with text() and html() methods:
$("#btn1").click(function() {
alert("Text: " + $("#test").text());
});
$("#btn2").click(function() {
alert("HTML: " + $("#test").html());
});
• The following example demonstrates how to get the
value of an input field with val() method:
$("#btn1").click(function() {
alert("Value: " + $("#test").val());
});
Get Attributes - attr()
• attr() method is used to get attribute values.
• The following example demonstrates how to get the
value of the href attribute in a link:
$("button").click(
function() {
alert($("#w3s").attr("href"));
}
);
Set Content and Attribute
• The following example demonstrates how to set content
with text(), html(), and val() methods:
$("#btn1").click(
function() {
$("#test1").text("Hello world!");
}
);
$("#btn2").click(
function() {
$("#test2").html("<b>Hello world!</b>");
}
);
$("#btn3").click(
function() {
$("#test3").val("Dolly Duck");
}
);
Callback Function for text(), html(), and val()
• Callback function has two parameters:
– index of the current element in the list of elements selected
– and the original (old) value.
• We can return string to use as new value from function.
$("#btn1").click(function() {
$("#test1").text(function(i, origText) {
return "Old text: " + origText + " New text: Hello! (index: " + i + ")";
});
});
$("#btn2").click(function() {
$("#test2").html(function(i, origText) {
return "Old html: " + origText + " New html: <b>Hello!</b> (index: " + i + ")";
});
});
Set Attributes - attr()
• attr() method is also used to set/change attribute values.
• The following example demonstrates how to change
(set) the value of the href attribute in a link:
$("button").click(
function() {
$("#w3s").attr("href", "http://www.w3schools.com/jquery");
}
);
Set Multiple Attributes
• attr() method also allows us to set multiple attributes at
the same time.
• The following example demonstrates how to set both the
href and title attributes at the same time:
$("button").click(
function() {
$("#w3s").attr( {
"href" : "http://www.w3schools.com/jquery",
"title" : "W3Schools jQuery Tutorial"
});
}
);
Callback Function for attr()
• Callback function for attr() has two parameters:
– the index of the current element in the list of elements selected
– and the original (old) attribute value.
• We can return string to use as new value from function.
$("button").click(
function() {
$("#w3s").attr("href", function(i, origValue) {
return origValue + "/jquery";
});
}
);
Add New HTML Content
• jQuery provides 3 methods to add new content:
– append() - inserts content at the end of the selected elements
– prepend() - inserts content at the beginning of the selected
elements
– after() - inserts content after the selected elements
– before() - inserts content before the selected elements
Append and Prepend Methods
• append() method inserts content AT THE END of the
selected HTML elements.
$("p").append("Some appended text.");
• prepend() method inserts content AT THE BEGINNING
of the selected HTML elements.
$("p").prepend("Some prepended text.");
Add Several New Elements with append()
and prepend()
• Both append() and prepend() methods can take an
infinite number of new elements as parameters.
function appendText()
{
var txt1 = "<p>Text.</p>";
// Create element with HTML
var txt2 = $("<p></p>").text("Text.");
// Create with jQuery
var txt3 = document.createElement("p"); // Create with DOM
txt3.innerHTML = "Text.";
$("p").append(txt1, txt2, txt3);
}
// Append the new elements
after() and before() Methods
• after() method inserts content AFTER the selected
HTML elements.
• before() method inserts content BEFORE the selected
HTML elements.
$("img").after("Some text after");
$("img").before("Some text before");
Add Several New Elements With after()
and before()
• Both after() and before() methods can take an infinite
number of new elements as parameters.
function afterText()
{
var txt1 = "<b>I </b>";
// Create element with HTML
var txt2 = $("<i></i>").text("love ");
// Create with jQuery
var txt3 = document.createElement("big"); // Create with DOM
txt3.innerHTML = "jQuery!";
$("img").after(txt1,txt2,txt3);
}
// Insert new elements after img
Remove Elements/Content
• To remove elements and content, there are mainly two
jQuery methods:
– remove() - removes selected element (and its child elements)
$("#div1").remove();
– empty() - removes child elements from selected element
$("#div1").empty();
Filter the Elements to be Removed
• remove() method also accepts one parameter, which
allows us to filter the elements to be removed.
• Parameter can be any of the jQuery selector syntaxes.
• The following example removes all <p> elements with
class="italic":
$("p").remove(".italic");
Get and Set CSS Classes
• jQuery has several methods for CSS manipulation as
follows:
– addClass() - adds one or more classes to the selected elements
– removeClass() - removes one or more classes from the selected
elements
– toggleClass() - toggles between adding/removing classes from
the selected elements
– css() - sets or returns the style attribute
Example Stylesheet
• The following stylesheet will be used for all the examples
of CSS:
.important
{
font-weight:bold;
font-size:xx-large;
}
.blue
{
color:blue;
}
addClass() Method
• The following example shows how to add class attributes
to different elements.
$("button").click(function() {
$("h1,h2,p").addClass("blue");
$("div").addClass("important");
});
• We can also specify multiple classes within addClass()
method:
$("button").click(function() {
$("#div1").addClass("important
blue");
});
removeClass() and toggleClass()
• The following example shows how to remove a specific
class attribute from different elements:
$("button").click(function() {
$("h1,h2,p").removeClass("blue");
});
• The following example will show how to use
toggleClass() method.
• This method toggles between adding/removing classes
from the selected elements:
$("button").click(function() {
$("h1,h2,p").toggleClass("blue");
});
css Method()
• css() method sets or returns one or more style properties
for the selected elements.
• To return the value of a specified CSS property, use the
following syntax:
css("propertyname");
• The following example will return background-color value
of the FIRST matched element:
$("p").css("background-color");
Set a css Property
• To set a specified CSS property, use following syntax:
css("propertyname","value");
• The following example will set background-color value
for ALL matched elements:
$("p").css("background-color","yellow");
• To set multiple CSS properties, use following syntax:
css({"propertyname":"value","propertyname":"value",...});
• Following example sets background-color and font-size
for ALL matched elements:
$("p").css({"background-color":"yellow","font-size":"200%"});
Introduction to AJAX
• AJAX is the art of exchanging data with a server, and
updating parts of a web page - without reloading the
whole page.
• AJAX = Asynchronous JavaScript and XML.
• AJAX is about loading data in the background and
display it on the webpage, without reloading the whole
page.
• Examples of applications using AJAX: Gmail, Google
Maps, Youtube, and Facebook tabs.
jQuery load() Method
• load() method is a simple, but powerful AJAX method.
• load() method loads data from server and puts returned
data into selected element.
$(selector).load(URL, data, callback);
– required URL parameter specifies the URL we wish to load.
– optional data parameter specifies a set of querystring key/value
pairs to send along with the request.
– optional callback parameter is the name of function to be
executed after load() is completed.
jQuery load() Method (cont)
• Here is the content of our example file: "demo_test.txt":
<h2>jQuery and AJAX is FUN!!!</h2>
<p id="p1">This is some text in a paragraph.</p>
• The following example loads the content of the file
"demo_test.txt" into a specific <div> element:
$("#div1").load("demo_test.txt");
• The following example loads the content of the element
with id="p1", inside the file "demo_test.txt", into a specific
<div> element:
$("#div1").load("demo_test.txt #p1");
load()’s Callback
• The optional callback parameter specifies a callback
function to run when load() is completed.
• The callback function can have different parameters:
– responseTxt - contains the resulting content if the call succeeds
– statusTxt - contains the status of the call
– xhr - contains the XMLHttpRequest object
$("button").click(function() {
$("#div1").load("demo_test.txt", function(responseTxt, statusTxt, xhr) {
if(statusTxt == "success")
alert("External content loaded successfully!");
if(statusTxt == "error")
alert("Error: "+xhr.status+": "+xhr.statusText);
});
});
HTTP Request: GET vs POST
• Two common methods for a request-response between
a client and server are: GET and POST.
– GET - requests data from a specified resource
– POST - submits data to be processed to a specified resource
• GET is used for just retrieving some data from server.
– Note: The GET method may return cached data.
• POST can be used to get some data from server.
– However, POST method NEVER caches data, and is often used
to send data along with the request.
$.get() Method
• The $.get() method requests data from server with an
HTTP GET request.
$.get(URL,callback);
– Required URL parameter specifies the URL you wish to request.
– Optional callback parameter is the name of a function to be
executed if request succeeds.
$.get() Example
• The following example uses the $.get() to retrieve data
from a file on server:
$("button").click(function() {
$.get("demo_test.php", function(data,status) {
alert("Data: " + data + "\nStatus: " + status);
});
});
– First parameter of $.get() is the URL we wish to request
("demo_test.php").
– Second parameter is a callback function.
– First callback parameter holds the content of the page requested,
and second callback parameter holds status of the request.
//demo_test.php
<?php echo "This is some text from an external PHP file." ?>
$.post() Method
• $.post() requests data from server using an HTTP POST
request.
$.post(URL, data, callback);
– Required URL parameter specifies URL we wish to request.
– Optional data parameter specifies some data to send along with
the request.
– Optional callback parameter is the name of function to be
executed if the request succeeds.
$.post() Example
• The following example uses $.post() method to send
some data along with the request:
$("button").click(function() {
$.post("demo_test_post.php", {
name:"Donald Duck",
city:"Duckburg"
},
function(data,status) {
alert("Data: " + data + "\nStatus: " + status);
});
});
//demo_test_post.php
<?php
$fname = $_POST ["name“];
$city
= $_POST["city“];
echo "Dear $fname.”;
echo "Hope you live well in $city.“;
?>