\nslide4. DOM Event Handlers HTML element event handlers fine for elements, not good for the entire webpage if it means adding handlers to every single element\nVarious DOM Event Handlers\nComplicated by different methods for different browsers and different versions
\n\n\n

\nslide5. Evolution – Make a event utility In file eventutility.js:\n\nvar eventUtility = {\n\taddEvent : (function() {\n\t\tif (typeof addEventListener === \"function\") {\n\t\t\treturn function(obj, evt, fn) {\n\t\t\t\tobj.addEventListener(evt, fn, false);\n\t\t\t};\n\t\t} else {\n\t\t\treturn function(obj, evt, fn) {\n\t\t\t\tobj.attachEvent(\"on\" + evt, fn);\n\t\t\t};\n\t\t}\n\t}()),\n\tremoveEvent : (function() {\n\t\tif (typeof addEventListener === \"function\") {\n\t\t\treturn function(obj, evt, fn) {\n\t\t\t\tobj.removeEventListener(evt, fn, false);\n\t\t\t};\n\t\t} else {\n\t\t\treturn function(obj, evt, fn) {\n\t\t\t\tobj.detachEvent(\"on\" + evt, fn);\n\t\t\t};\n\t\t}\n\t}())\n};
\nslide6. HTML / JavaScript Code
\n\n\n
\n\n\n
\nslide7. Accessing the Event Target Use event.type and event.target to determine the event and target of the event
\n\n\n
\n\n\n
\nslide8. Event Target Can use the same event handler for multiple targets
\n\n
\n\n
\n\n\n\n
\nslide9. AJAX Term invented by Jesse Garrett, 2005, “Ajax: A New Approach to Web Applications”\nAsynchronous JavaScript + XML\nAlthough XML still used, other formats also used as well\nIn general, the use of JavaScript to send and receive data using HTTP without reloading the page\nAllows for dynamic pages without clunky submit/reload paradigm
\nslide10. AJAX and the XHR Object XHR = XMLHttpRequest Object\nOriginated as a component, XmlHttp, in Microsoft’s MSXML Library\nStill necessary if you’re programming for old versions of IE\nBuilt into modern browsers\nDespite the XML name you can retrieve more than XML and is commonly used with plaintext\nMust be used with a HTTP server\nCreating an XHR obje" }

Intro to JavaScript Events JavaScript Events

Published  . 0 views
↓ Download
Intro to JavaScript Events JavaScript Events
1 / 1
Intro to JavaScript Events JavaScript Events - slide 1 of 18 Intro to JavaScript Events JavaScript Events - slide 2 of 18 Intro to JavaScript Events JavaScript Events - slide 3 of 18 Intro to JavaScript Events JavaScript Events - slide 4 of 18 Intro to JavaScript Events JavaScript Events - slide 5 of 18 Intro to JavaScript Events JavaScript Events - slide 6 of 18 Intro to JavaScript Events JavaScript Events - slide 7 of 18 Intro to JavaScript Events JavaScript Events - slide 8 of 18 Intro to JavaScript Events JavaScript Events - slide 9 of 18 Intro to JavaScript Events JavaScript Events - slide 10 of 18 Intro to JavaScript Events JavaScript Events - slide 11 of 18 Intro to JavaScript Events JavaScript Events - slide 12 of 18 Intro to JavaScript Events JavaScript Events - slide 13 of 18 Intro to JavaScript Events JavaScript Events - slide 14 of 18 Intro to JavaScript Events JavaScript Events - slide 15 of 18 Intro to JavaScript Events JavaScript Events - slide 16 of 18 Intro to JavaScript Events JavaScript Events - slide 17 of 18 Intro to JavaScript Events JavaScript Events - slide 18 of 18
Description: Intro to JavaScript Events JavaScript Events Events in JavaScript let a web page react to some type of input Many different ways to handle events due to historyvendor differences but we have a generally standard way now Will not cover all

Related Topics

Download Presentation

"Intro to JavaScript Events JavaScript Events" is the property of its rightful owner. Permission is granted to download and print the materials on this website for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.

Presentation Transcript

slide1. Intro to JavaScript Events<br>
slide2. JavaScript Events Events in JavaScript let a web page react to some type of input
Many different ways to handle events due to history/vendor differences but we have a generally standard way now
Will not cover all event levels
Events
W3C DOM Standard; attach to HTML elements
DOM Levels 0 to 3<br>
slide3. Event Handlers to HTML Attributes We can add an event handler to a HTML element
onkeydown, onkeyup, onkeypress
onclick, onmouseover, onmouseout, onmousedown, onmouseup
Others… <form name="theForm" action="">
<input type=text name="myTextBox" id="text1" value="1" onclick="addOne();">
</form>

<div id="myDiv" onmouseover="changeColor(this,'red');"
onmouseout="changeColor(this,'black');">
Hello there
</div> <script type="text/javascript">
function addOne()
{
var el = document.theForm.myTextBox;
el.value = parseInt(el.value) + 1;
}

function changeColor(el, col)
{
el.style.color = col;
}
</script><br>
slide4. DOM Event Handlers HTML element event handlers fine for elements, not good for the entire webpage if it means adding handlers to every single element
Various DOM Event Handlers
Complicated by different methods for different browsers and different versions <form name="theForm" action="">
<input type=button name="myButton" id="theButton" value="Click Me">
<input type=text name="myTextBox" id="text1" value="1">
</form> <script type="text/javascript">
var btn = document.theForm.myButton;
if (typeof addEventListener === "function")
{
btn.addEventListener("click", addOne, false); // Compliant browsers
}
else
{
btn.attachEvent("onclick", addOne); // IE8 and lower
}

function addOne()
{
var el = document.theForm.myTextBox;
el.value = parseInt(el.value) + 1;
}
</script><br>
slide5. Evolution – Make a event utility In file eventutility.js:

var eventUtility = {
addEvent : (function() {
if (typeof addEventListener === "function") {
return function(obj, evt, fn) {
obj.addEventListener(evt, fn, false);
};
} else {
return function(obj, evt, fn) {
obj.attachEvent("on" + evt, fn);
};
}
}()),
removeEvent : (function() {
if (typeof addEventListener === "function") {
return function(obj, evt, fn) {
obj.removeEventListener(evt, fn, false);
};
} else {
return function(obj, evt, fn) {
obj.detachEvent("on" + evt, fn);
};
}
}())
};<br>
slide6. HTML / JavaScript Code <form name="theForm" action="">
<input type=button name="myButton" id="theButton" value="Click Me">
<input type=text name="myTextBox" id="text1" value="1">
</form>

<script type="text/javascript" src="eventutility.js"></script>
<script type="text/javascript">
var btn = document.theForm.myButton;
eventUtility.addEvent(btn, "click", addOne);

function addOne()
{
var el = document.theForm.myTextBox;
el.value = parseInt(el.value) + 1;
}
</script><br>
slide7. Accessing the Event Target Use event.type and event.target to determine the event and target of the event <form name="theForm" action="">
<input type=button name="myButton" id="theButton" value="Click Me">
<input type=text name="myTextBox" id="text1" value="1">
</form>

<script type="text/javascript" src="eventutility.js"></script>
<script type="text/javascript">
var btn = document.theForm.myButton;
eventUtility.addEvent(btn, "click", eventHandler);

function eventHandler(event)
{
alert(event.type);
event.target.style.backgroundColor = "green";
}
</script><br>
slide8. Event Target Can use the same event handler for multiple targets <form name="theForm" action="">
<input type=button name="myButton" id="theButton" value="Click Me">
<input type=text name="mainTextBox" id="text0" value="" width=20><br/>
<input type=text name="myTextBox" id="text1" value="1">
</form>

<script type="text/javascript" src="eventutility.js"></script>
<script type="text/javascript">
var btn = document.theForm.myButton;
eventUtility.addEvent(btn, "click", eventHandler);

var txt = document.theForm.text0;
eventUtility.addEvent(txt, "keypress", eventHandler);

function eventHandler(event)
{
if (event.type == "keypress") {
document.theForm.myTextBox.value = parseInt(document.theForm.myTextBox.value) + 1;
}
else if (event.type == "click") {
alert("You clicked on " + event.target.id);
}
}
</script><br>
slide9. AJAX Term invented by Jesse Garrett, 2005, “Ajax: A New Approach to Web Applications”
Asynchronous JavaScript + XML
Although XML still used, other formats also used as well
In general, the use of JavaScript to send and receive data using HTTP without reloading the page
Allows for dynamic pages without clunky submit/reload paradigm<br>
slide10. AJAX and the XHR Object XHR = XMLHttpRequest Object
Originated as a component, XmlHttp, in Microsoft’s MSXML Library
Still necessary if you’re programming for old versions of IE
Built into modern browsers
Despite the XML name you can retrieve more than XML and is commonly used with plaintext
Must be used with a HTTP server
Creating an XHR object:
var xhr = new XMLHttpRequest();<br>
slide11. XHR Object Call the open method
xhr.open("GET", "info.txt", true);

Asynchronously retrieve “info.txt” from the same website/port, can also use POST
Five ready states
0: Object created but not initialized
1: Object initialized but request not sent
2: Request sent
3: Response received from HTTP server
4: Requested data fully received
Same response codes as HTTP
2xx = Success, 4xx = Client Error, 5xx = Server Error<br>
slide12. Sample XHR Code <html>
<header>
<title>This is a test</title>
</header>

<body>
<H1>Testing</H1>

<script>
var xhr = new XMLHttpRequest();
xhr.open("GET", "info.txt", true);

xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
alert("response received: " + xhr.responseText);
}
}
xhr.send(null);

</script>

</body>
</html><br>
slide13. Ajax POST Use POST requests to send data and receive a response; couple with events for dynamic page <form name="theForm" action="">
<input type=text name="myTextBox" id="text1" value=""><br/>
<div id="suggestion">Suggestion goes here from server</div>
</form>

<script type="text/javascript" src="eventutility.js"></script>
<script type="text/javascript">
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
processResponse(xhr.responseText);
}
} var txt = document.theForm.text1;
eventUtility.addEvent(txt, "keyup", eventHandler);

// When we press a key send to the server like it is a <form>
// and wait for a response
function eventHandler(event)
{
var data = "myTextBox=" +
encodeURIComponent(document.theForm.myTextBox.value) +
"&otherParameter=someValue";
xhr.open("POST", "ajax_test.php");
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(data);
}

// Display response from server in DIV
function processResponse(responseData)
{
var el = document.getElementById("suggestion");
el.innerHTML = "<B>" + responseData + "</B>";
}
</script><br>
slide14. Ajax Server Side PHP <?php
if (isset($_REQUEST['myTextBox']))
{
// Normally you would do some more interesting lookup than this
// canned example
$txt= strtolower($_REQUEST['myTextBox']);
if (strlen($txt)>0)
{
$firstLetter = $txt[0];
if ($firstLetter == 'a')
print "Alfred";
else if ($firstLetter == 'k')
print "Kenrick";
else if ($firstLetter == 'b')
print "Bob";
else if ($firstLetter == 'j')
print "Jose";
}
else
print "";
}
else
print "";
?><br>
slide15. jQuery jQuery is a popular JavaScript library that makes it easier to navigate the document, handle events, animations, etc.
https://jquery.com
There are a variety of UI libraries built on jQuery as well, e.g. https://jqueryui.com
Example: Serious Fun Score Tracker built in jQuery Mobile
http://www.cse.uaa.alaska.edu/~afkjm/fun<br>
slide16. jQuery Taste <html>
<head>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script>
function handleButton()
{
txt = $('#text1').val();
if (txt == "")
$('#suggestion').html("<B>The value may not be empty!</B>");
}
</script>
</head>

<body>

<form name="theForm" action="">
<input type=text name="myTextBox" id="text1" value="">
<input type="button" name="myButton" id="button1" value="Click"
onClick = "handleButton()"><br/>
<div id="suggestion"></div>
</form> Instead of
getElementByID

if () then read, otherwise
if contents then write<br>
slide17. jQuery Server Submission <html>
<head>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script>
function handleButton()
{
txt = $('#text1').val();
if (txt == "")
$('#suggestion').html("<B>The value may not be empty!</B>");
else
{
$.get('jquery-submit.php', { user: txt}) .done(function(data) {
$('#suggestion').html(data);
});
}
}
</script>
</head>

<body> <form name="theForm" action="">
<input type=text name="myTextBox" id="text1" value="">
<input type="button" name="myButton" id="button1" value="Click"
onClick = "handleButton()"><br/>
<div id="suggestion"></div>
</form><br>
slide18. PHP Server Code for jQuery example <?php
if (isset($_REQUEST['user']))
{
// Normally you would do some more interesting lookup than this
// canned example
$txt= strtolower($_REQUEST['user']);
if (strlen($txt)>0)
{
if ($txt== 'kenrick')
print "30240421";
else if ($txt== 'bob')
print "30150523";
else if ($txt== 'jose')
print "30429382";
}
else
print "";
}
else
print "";
?><br>