Beruflich Dokumente
Kultur Dokumente
Errors
try
catch
throw
finally
JavaScript
Code
try {
adddlert("Welcome guest!");
}
catch(err) {
console.log(err.message);
}
JavaScript
Errors: Throw
Code
// throw a text
// throw a number
JavaScript
Code
If the value is wrong, an
exception (err) is thrown.
The exception (err) is caught by
the catch statement and a
custom error message is
displayed.
function myFunction() {
var message, x;
message = document.getElementById("message");
message.innerHTML = "";
x = document.getElementById("demo").value;
try {
x = Number(x);
if(x == "") throw "empty";
if(isNaN(x)) throw "not a number";
if(x > 10) throw "too high";
if(x < 5) throw "too low";
}
catch(err) {
message.innerHTML = "Input is " + err;
}
}
JavaScript
Errors: Finally
Code
The finally statement lets you
execute code, after try and
catch, regardless of the result.
try {
Block of code to try
}
catch(err) {
Block of code to handle errors
}
finally {
Code to be executed regardless of the try / catch result
}
JavaScript
Errors: Finally
Code
Explain this.
function myFunction() {
var message, x;
message = document.getElementById("message");
message.innerHTML = "";
x = document.getElementById("demo").value;
try {
x = Number(x);
if(x == "") throw "is empty";
if(isNaN(x)) throw "is not a number";
if(x > 10) throw "is too high";
if(x < 5) throw "is too low";
}
catch(err) {
message.innerHTML = "Error: " + err + ".";
}
finally {
document.getElementById("demo").value = "";
}
}
JavaScript
Errors: Debugger
Code
var x = 15 * 5;
debugger;
document.getElementbyId("demo").innerHTML = x;
Result
JSON
JavaScript
Code
{"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna",
"lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]}
JavaScript
JSON Rules
Code
"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]
JavaScript
jQuery
jQuery is a JS Library
jQuery Intro
jQuery greatly
simplifies JS
programming
Example
$(document).ready(function(){
$("p").click(function(){
$(this).hide();
});
});
jQuery is easy to
learn
jQuery
Example
Download and connect the
library to head section.
<head>
<script src="jquery-1.11.2.min.js"></script>
</head>
Example
Or use CDN for it.
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min
.js"></script>
jQuery
Syntax
$(#demo).hide();
Basic syntax
In jQuery you select (query)
HTML elements and perform
"actions" on them.
(selector)
action()
Define/access jQuery
To do something with
the selected element(s)
jQuery
Syntax
Example
$(this).hide();
$("p").hide();
$(".test").hide();
$("#test").hide();
jQuery
Document.ready
Code
$(document).ready(function(){
// code goes here...
});
Code
$(function(){
// jQuery methods go here...
});
jQuery
Selectors
Code
jQuery selectors are based on
CSS selectors and have almost
the same syntax.
// id selector
$("#test");
// class selector
$(".test");
// tag selector
$("p");
jQuery
Selectors
Syntax
Description
$("*")
$(this)
$("p.intro")
$("p:first")
$("ul li:first")
$("ul li:first-child")
$("[href]")
$(":button")
$("tr:even")
$("tr:odd")
Events
jQuery
Mouse Events
Keyboard Events
Form Events
Document/Window Events
click
keypress
submit
load
dblclick
keydown
change
resize
mouseenter
keyup
focus
scroll
blur
unload
mouseleave
jQuery
$("p").click();
Code
$("p").click(function(){
// action
});
Code
The dblclick() method attaches
an event handler function to an
HTML element.
$("p").dblclick(function(){
$(this).hide();
});
jQuery
Events
Code
$("#p1").mouseenter(function(){
alert("You entered p1");
});
Code
The mouseleave() method
attaches an event handler
function to an HTML element.
$("#p1").mouseleave(function(){
alert("You leave p1");
});
Code
The mousedown() method
attaches an event handler
function to an HTML element.
$("#p1").mousedown(function(){
alert("Mouse down over p1");
});
jQuery
Events
Code
$("#p1").mouseup(function(){
alert("Mouse up over p1!");
});
Code
$("#p1").hover(function(){
alert("You entered p1!");
},
function(){
alert("You now leave p1!");
});
jQuery
Events
Code
$("input").focus(function(){
$(this).css("background-color", "#cccccc");
});
Code
The blur() method attaches an
event handler function to an
HTML form field (loses focus).
$("input").blur(function(){
$(this).css("background-color", "#ffffff");
});
jQuery
$("#hide").click(function(){
$("p").hide();
});
$("#show").click(function(){
$("p").show();
});
Code
The optional callback parameter
is a function to be executed after
the hide() or show() method
completes (you will learn more
about callback functions in a
later chapter).
$("button").click(function(){
$("p").hide(1000);
});
jQuery
toggle()
Code
With jQuery, you can toggle
between the hide() and show()
methods with the toggle()
method.
Shown elements are hidden and
hidden elements are shown
$("button").click(function(){
$("p").toggle();
});
Method
Description
animate()
Description
clearQueue()
finish()
hide()
queue()
show()
slideDown()
slideToggle()
slideUp()
stop()
toggle()
delay()
dequeue()
fadeIn()
fadeOut()
fadeTo()
fadeToggle()
Effects