Events Part II Browser Variations Document / Window Events Form Events --- Using named functions (instead of anonymous)

Slides:



Advertisements
Similar presentations
DEVELOPING ICT SKILLS PART -TWO
Advertisements

Modifying existing content Adding/Removing content on a page using jQuery.
Events Part III The event object. Learning Objectives By the end of this lecture, you should be able to: – Learn to use the hover() function – Work with.
FrontPage Express By John G. Summerville Ph.D.©, RN.
CS7026 jQuery Events. What are Events?  jQuery is tailor-made to respond to events in an HTML page.  Events are actions that can be detected by your.
2-May-15 GUI Design. 2 HMI design There are entire college courses taught on HMI (Human-Machine Interface) design This is just a very brief presentation.
Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.
Form Validation. Learning Objectives By the end of this lecture, you should be able to: – Describe what is meant by ‘form validation’ – Understand why.
Welcome to Florida International University Online J.O.B.S. Link Applicant Tutorial.
Welcome to the Brookdale Community College Online Employment System Applicant Tutorial.
Welcome to the Ivy Tech Community College Online Employment System Applicant Tutorial.
Escaping Characters document.write(). Learning Objectives By the end of this lecture, you should be able to: – Recognize some of the characters that can.
Downloading and Installing AutoCAD Architecture 2015 This is a 4 step process 1.Register with the Autodesk Student Community 2.Downloading the software.
1 Welcome to the Colgate University Online Employment System Applicant Tutorial.
Yahoo Tutorial This tutorial aims to quickly cover some of the basic elements of web based using Yahoo - a free service Use the Index.
Introduction to JavaScript. Aim To enable you to write you first JavaScript.
Hotmail Tutorial This tutorial aims to quickly cover some of the basic elements of web based using msn Hotmail - a free service Use the Index.
Welcome to the University of Florida Online Employment System Applicant Tutorial.
Welcome to And the Rice University Online Job Application Tutorial.
Welcome to the Alaska Statewide System Online Employment System Applicant Tutorial.
Welcome to the Southeastern Louisiana University’s Online Employment Site Applicant Tutorial!
Downloading and Installing PAF Insight PAF Insight can be easily downloaded Or can be installed from a CD A license is needed t0 activate the program.
CNIT 133 Interactive Web Pags – JavaScript and AJAX Event and Event Handling.
Welcome to the University of West Florida Online Employment System Applicant Tutorial.
Microsoft Windows LEARNING HOW USE AN OPERATING SYSTEM 1.
JQuery and Forms – Part I. Learning Objectives By the end of this lecture, you should be able to: – Retrieve the values entered by a user into a form.
KRAD makes it easy to handle HTML events in your webpage. Kuali University: Apply Now Lab 6: Fun with HTML Events Lab Objectives HTML Events – what are.
Selectors. Learning Objectives By the end of this lecture, you should be able to: – Select items using jQuery based on ID, class name, or HTML tag. –
Microsoft PowerPoint Getting Started Guide Prepared for Towson University Dr. Jeff M. Kenton Amy Chase Martin 2007.
Using the API. Learning Objectives By the end of this lecture, you should be able to: – Identify what is meant by an ‘API’ – Know how to look up functions.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
CSS Class 7 Add JavaScript to your page Add event handlers Validate a form Open a new window Hide and show elements Swap images Debug JavaScript.
1 IE in the Classroom The Internet Explorer Web Browser EDW647 Internet for Educators Roger Webster, Ph.D. Millersville University Department of Computer.
Introduction to Arrays. definitions and things to consider… This presentation is designed to give a simple demonstration of array and object visualizations.
‘Tirgul’ # 7 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #7.
Lecture # 6 Forms, Widgets and Event Handling. Today Questions: From notes/reading/life? Share Personal Web Page (if not too personal) 1.Introduce: How.
INTRODUCTION TO HTML5 Using jQuery with HTML5. Introducing jQuery  Although it is not a part of any W3C or WHATWG specification, jQuery performs an important.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
CO1552 Web Application Development HTML Forms, Events and an introduction to JavaScript.
Downloading and Installing Autodesk Revit 2016
76 © 1998, 1999, 2000 David T. Gray, Howard Duncan, Jane Kernan Frames When displaying information in a browser, it is sometimes useful to divide the display.
Animation & Effects Using JQuery. What is jQuery? jQuery is a lightweight, JavaScript library. The purpose of jQuery is to make it much easier to use.
Working with Files. Learning Objectives By the end of this lecture, you should be able to: – Examine a file that contains code with unfamiliar material,
Downloading and Installing Autodesk Inventor Professional 2015 This is a 4 step process 1.Register with the Autodesk Student Community 2.Downloading the.
Modifying HTML attributes and CSS values. Learning Objectives By the end of this lecture, you should be able to: – Select based on a class (as opposed.
CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.
IS2802 Introduction to Multimedia Applications for Business Lecture 3: JavaScript and Functions Rob Gleasure
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
Event Handling. Objectives Using event handlers Simulating events Using event-related methods.
Events Part I Mouse Events. Learning Objectives By the end of this lecture, you should be able to: – Understand what is meant by an ‘event’ – Appreciate.
Project 5: Using Pop-Up Windows Essentials for Design JavaScript Level One Michael Brooks.
Changing HTML Attributes each() function Anonymous Functions $(this) keyword.
Review of HTML and CSS A (very) brief review of some key fundamentals to be aware of in IT-238.
Introduction to Arrays. Learning Objectives By the end of this lecture, you should be able to: – Understand what an array is – Know how to create an array.
Computer Basics Tutorial Text boxes like this will give you information. Boxes like this at the bottom of a slide will give you instructions. Press the.
Web Programming Java Script-Introduction. What is Javascript? JavaScript is a scripting language using for the Web. JavaScript is a programming language.
Weebly Elements, Continued
Retrieving information from forms
Unit 27 - Web Server Scripting
Introduction to TouchDevelop
JavaScript and Events CS Programming Languages for Web Applications
CS7026 jQuery Events.
JavaScript and Events CS Programming Languages for Web Applications
Events Part I Mouse Events.
Events Part II Browser Variations Document / Window Events Form Events
Events Part III The event object.
Using the API.
JavaScript Part 2 Organizing JavaScript Code into Functions
Retrieving information from forms
Presentation transcript:

Events Part II Browser Variations Document / Window Events Form Events --- Using named functions (instead of anonymous)

Learning Objectives By the end of this lecture, you should be able to: – Begin to appreciate the significance/challenges of browser variability to a web developer – Understand the basics of document and window events – Understand the basics of form events and keyboard events

Okay, we need to digress for a moment… One of the biggest issues in web design is that in spite of the W3C’s best efforts at standardization, the fact remains that some browsers behave differently from others in responding to identical blocks of HTML/JS/jQuery/CSS code. In fact, one of main the reasons that jQuery was develped was because one bane of JS programmers’ existence was the hours and hours of extra coding they had to write to check for different browsers so that they could modify their code based on the way different browsers worked. In addition, even with a given browser (e.g. Firefox or Safari), different versions would be in use on different computers which would frequently respond in different ways. jQuery attempts to minimize many of these issues. When you invoke a function such as fadeIn(), there is other stuff that jQuery is doing behind the scenes to ensure that regardless of which browser you are using, the function should behave as expected. However, there remain certain types of functionality that do behave quite differently depending on which browser/version is being used. Examples of events to which different browsers/versions frequently behave inconsistently include the initial loading and unloading of web pages into the browser window, and when windows are being resized. So what do we do? Simple: For the most part, we avoid them! Okay, I may be exaggerating a little here. However, if and when you use any of these tools, they should be used with some care. Definitely do a little poking around the API and do a LOT of testing with different browsers before you go ‘live’ with a major website. Browser differences are one of the biggest issues that serious web designers spend their time and energy trying to anticipate and respond to! BROWSER WARS!!!

Document / Window Events There are a series of events that fire based on web pages being loaded into the browser window, browser windows being resized, web pages being unloaded (i.e. user clicks to another page), and so on. We’ll briefly discuss them here, although we will not delve into them very much for now. As with some of the events discussed in previous lectures, most of the time, the name of the function corresponds to the name of the event. For example, the ‘unload’ event can be matched to a function called unload(). The next couple of slides do discuss this method along with a corresponding ‘ load() ’ method. I will leave these examples in for now, but will be removing them in subsequent sessions of this course.

Document / Window Events There are a series of events that fire based on web pages being loaded into the browser window, browser windows being resized, web pages being unloaded (i.e. user clicks to another page), and so on. We’ll briefly discuss them here, although we will not delve into them very much for now. As with some of the events discussed in previous lectures, most of the time, the name of the function corresponds to the name of the event. For example, the ‘unload’ event can be matched to a function called unload(). The unload event – DEPRECATED in jQuery version 1.8 Whenever the user does something to leave the page, whether it be clicking on a link to another page, closing the browser window, or closing a browser tab this event will fire. If you do want to react to a page being unloaded, you can do so with a related function helpfully named unload() : //Note that the selector is called ‘window’ (not ‘document’) $(window).unload(function() { alert('Goodbye!'); }); This function has been used for both good and evil. On the one hand, it can be used to clean up any last minute details – particularly if there has been server-side interaction. Another popular use is to warn the user if they have failed to do something like clicking the ‘submit’ button for a form. On the other hand, this has been abused by programmers to prevent users from easily leaving their web pages! For example, they might write code that will simply open up a new window with the same page on it! Needless to say, such developers are deserving of their own private wing at Guantanamo. The unload() function behaves in different ways depending on which browser is being used. Students trying it in Firefox, for example, may find that it does not work. If you are one of these students, do not worry about it for now. We may return to the specifics at a later point.

Document / Window Events contd: The ‘load’ event The load event – also deprecated  stick with $(document).ready()

Document / Window Events contd: Resize & Scroll events The resize event Whenever the user drags their window to resize it, or if they click the maximize or minimize buttons, this event will fire. You can respond to this event with the resize() function: $(window).resize(function() { alert('Help, I\'m shriking (or growing)!'); //Note the escape character!! }); You will definitely want to be careful with this one! The reason is that in some browsers, the event fires only once per resize that the user makes. In other browsers, the event fires constantly as the user resizes the window. Imagine if you had an alert() function as the body of the resize() function. In some cases, you could end up with hundreds of alert boxes poppping up! On those occasions when this function is used, it is usually used to reformat a web page based on the new size. The scroll event This event fires whenever the user moves a scroll bar. It also typically (based on which browser!) will respond to up and down arrow keys and special keys such as home/end/page-up/page-down and so on. It may also respond to the scroll wheel of a mouse, or a two-fingered drag on a mousepad.

Form Events Forms, too, generate a series of events behind the scenes. For example, every time you click into a text box (e.g. to enter your address), a ‘focus’ event fires. The ‘focus’ event: This event fires whenever a user clicks in a text-box, radio button, checkbox, and so on. For example, you could have a little window pop up letting the user know what they want to do. The function’s name is called focus() and it’s argument is, as usual, an anonymous function. Example: Say you had a text field with an id of ‘ txtUser ’. The following code would cause the background color of the text field to become yellow: $('#txtUser ').focus(function() { $(this).css('background-color','yellow'); //Note the use of ‘this’ here //Remember that ‘this’ is a substitute for the selector. You SHOULD get used to using ‘this’ }); The ‘blur’ event: This event is fired when a form element that was previously ‘focused’ is left. So say a user has just finished typing something in an text field called ‘txt ’. When the user clicks on the next element of the form, the ‘txt ’ element will fire the ‘blur’ event. In other words, the ‘blur’ event is fired when an element is no longer infocus. Let’s use it to change the previous example (where we had the ‘txtUser ’ highlighted in yellow) to return back to white: $('#txtUser ').blur(function() { $(this).css('background-color','white'); }); The blur event is also very helpful for form validation. For example, you could write code to check to make sure that has only entered valid characters in, say, a field for phone number.

Form Events contd The ‘submit’ event: Fires when the user clicks on a ‘submit’ button. This is commonly used for form validation. That is, when the user submits the form, a script kicks in that looks through all of the form elements to make sure that all required elements have been completed, and that no invalid characterrs (e.g. for hacking purposes) have been entered. The ‘change’ event: This only works for certain form elements such as radio buttons and checkboxes. This event fires whenever the element has been changed. So if a checkbox that was previously checked is now unchecked by the user, the change event will fire. Programmers sometimes use this event to immediately react to a form event instead of the submit or blur events. Choosing submit v.s. blur v.s. change for form validation will depend on the context and various requirements. We may investigate this further down the road when we take a closer look at forms and regular expressions.

Keyboard Events Keyboard events are tricky to use because they are generated differently depending on which browser is in use. However, they do have their uses. For example, some videos allow the user to press the space bar to pause and restart. If you want to use keyboard events, you should definitely spend a little time exploring the API documentation. The keypress event This is similar to the mousedown event. That is, the moment you press a key down (and even before you release the key), this event fires. For most browsers, this event fires over and over until you release the key. If you had an online ‘shoot-em-up’ video game, you could use the keypress to, say, have a gun fire over and over as long as the user holds the key down. The keydown event This is very similar to the keypress event. However, in most browsers, it is fired only once as opposed to repeatedly. One subtle distinction from keypress is that this event is fired just before keypress. So if you happened to have code written for both of these events (keydown and keypress), you should realize that the keydown event will fire first. The keyup event Not surprisingly, this event is fired when you release the key. It is not used very often.

Unrelated Topic: Using a named function instead of an anonymous function Using an Anonymous Function A little while back, we learned how to use anonymous functions in certain situations such as reponding to events: $('selector').onclick( function() { document.write('Hello, User'); document.write(' How are you this fine day? '); } ); However, if you anticipate the possibility that you may need to use the code for your function at some other point, then you should take that code and place it inside its own function. This is nothing new – you have been placing code inside functions all along, right up until we learned aboyt anonymous functions. Using a Named Function In this case, you would take your code and placed it inside a named function (as you’ve done many times in the past): function greetUser () { document.write('Hello, User'); document.write(' How are you this fine day? '); } Now you can simply invoke the function as the argument: $('selector').onclick( greetUser );

Using a named function instead of an anonymous function contd. Using a Named Function $('selector').onclick( greetUser ); However, there is one VERY important detail that you must note. Did you spot it??? When you invoke the function as the argument to onclick (or to any event), you do NOT include the parentheses. Note that after we write the name of the function ‘greetUser’, there are NO parentheses! This is not typical behavior, and therefore may be easy to forget. It’s one of those annoying things that you simply have to remember!

Named or Anonymous? Which should you use? It depends. If you are only planning on using the code at that time and you do not anticipate using it at any other point, then it is probably better to use an anonymous function. This way, the code is right there in one place and easy to follow. If however, you anticipate the possibility of using the same code two or more times, then you should place it inside a named function.