Info2180 Lecture 6
Info2180 Lecture 6
http://jquery.com/
ADDING JQUERY TO YOUR HTML PAGE
<html>
<head>
<script src="https://code.jquery.com/
jquery-3.6.1.min.js"></script>
<script src="myscripts.js"></script>
</head>
<body>
<h1>My Web page</h1>
</body>
</html>
JQUERY OBJECT
$("p")
// or
jQuery("p")
JQUERY DOCUMENT READY
$( document ).ready(function() {
});
SELECTING ELEMENTS WITH JQUERY
$("a").click(function() {
console.log("You clicked a link!");
});
$("a").on("click", function() {
console.log("You clicked a link");
});
You can reference any event handlers (e.g. mouseover, keypress, etc.)
JQUERY ADD AND REMOVE CLASSES
$("a").addClass("test");
$("a").removeClass("test");
JQUERY SET AND GET ATTRIBUTES
$("div").attr("id", "my-id");
$("a").attr("href");
JQUERY
JQUERY EFFECTS
AJAX OVERVIEW
fi
SYNCHRONOUS COMMUNICATION
ASYNCHRONOUS COMMUNICATION
MAKING AN AJAX REQUEST
httpRequest.onreadystatechange = doSomething;
httpRequest.open('GET', url);
httpRequest.send();
HANDLING THE RESPONSE TO AN AJAX REQUEST
function doSomething() {
if (httpRequest.readyState === XMLHttpRequest.DONE) {
if (httpRequest.status === 200) {
let response = httpRequest.responseText;
alert(response);
} else {
alert('There was a problem with the request.');
}
}
}
JQUERY AJAX EXAMPLE
$.ajax("somepage.php")
.done(function(result) {
$("#weather-temp").html("<strong>" + result +
"</strong> degrees");
}).fail(function(result) {
$("#message").html("There seems to be an error.");
});
FETCH API EXAMPLE
fetch('https://example.com/somepage.php')
.then(data => {
console.log(data)
})
.catch(error => {
console.log(error);
});
HANDLING ASYNCHRONOUS
OPERATIONS IN JAVASCRIPT
CALLBACKS
TEXT
CALLBACKS
setTimeout(function(){
// runs after 2 seconds
}, 2000)
window.addEventListener('load', function(){
// window loaded
// do what you want
})
THE PROBLEM WITH CALLBACKS
doSomething(function(result) {
doSomethingElse(result, function(newResult) {
doThirdThing(newResult, function(finalResult) {
console.log('Got the final result: ' + finalResult);
}, failureCallback);
}, failureCallback);
}, failureCallback);
AN EXAMPLE OF CALLBACK HELL
window.addEventListener('load', () => {
document.getElementById('button')
.addEventListener('click', () => {
setTimeout(() => {
items.forEach(item => {
// your code here
})
}, 2000)
})
})
CALLBACKS
ALTERNATIVES TO CALLBACKS
PROMISES
▸ Promises are JavaScript objects that represent the eventual result of an
asynchronous operation.
PROMISES
isItAGreatDay(greatDay)
.then((resolvedValue) => {
console.log(resolvedValue);
// Do something with the successful value
})
.catch((error) => {
console.log(error);
// Do something with the rejected/error value
});
ASYNC...AWAIT
ASYNC/AWAIT
ASYNC/AWAIT
ASYNC...AWAIT
▸ You declare an async function with the keyword async.
▸ Then within the function we would use the await operator to pause
the execution of the function until our asynchronous action completes.
console.log('Before')
doSomething()
console.log('After')
EXAMPLE USING FETCH API USING PROMISES
VALIDATION TYPES
CLIENT-SIDE VALIDATION
▸ trim() function
▸ length property
console.log(stringWithSpaces.trim());
//-> hello
console.log(someString.length);
//-> 5
console.log(exp.test(telephoneNumber));
//-> true
https://developer.mozilla.org/en/docs/Web/
JavaScript/Guide/Regular_Expressions
JQUERY, AJAX AND FORM VALIDATION
▸ ^ means begins with (e.g. /^A/ will match the A in "Apple" but
not "an Apple".
▸ $ means ends with. (e.g. /t$/ will match the t in "hot" but not
"hotter".
▸ [] group in a set; So it will match any single character from the set.
e.g. /[bcm]at/ would match strings containing "bat", "cat", "mat"
JQUERY, AJAX AND FORM VALIDATION
▸ empty()
▸ filter_input()
▸ filter_var()
▸ strlen()
▸ htmlentities()
▸ preg_match()
EXAMPLE OF EMPTY()
$name = $_POST['fname'];
if (empty($name)) {
} else {
echo $name;
} else {
echo("Email is valid");
if (!filter_var($myEmail, FILTER_VALIDATE_EMAIL)) {
} else {
echo("Email is valid");
echo htmlentities($str);
// Outputs: A 'quote' is <b>bold</b>
$telephone = "876-999-1234";
if (!preg_match("/^\d{3}-\d{3}-\d{4}$/", $telephone)) {
RESOURCES
▸ jQuery - http://jquery.com/
▸ AJAX - https://developer.mozilla.org/en-US/docs/AJAX
▸ Async/Await - https://developer.mozilla.org/en-US/docs/Learn/JavaScript/
Asynchronous/Async_await
JQUERY, AJAX AND FORM VALIDATION
RESOURCES