Event driven programming
Video: Event driven programmingRight now all our code executes as soon as the page loads our script file. It runs through it as fast as possible and finishes. But that's not good enough anymore. What I want to be able to do is start to react to some input from the user. And by input, I don't just mean prompting them for their name, but just being able to react to a whole bunch of different things. Does the user clicks somewhere on the screen, does the user resize the window, do they move the mouse? What do they do? What I want to do is start responding to events.
- Where to go from here
Viewers: in countries Watching now:
Finally, the course compares how code is written in several different languages, the libraries and frameworks that have grown around them, and the reasons to choose each one.
- Writing source code
- Understanding compiled and interpreted languages
- Requesting input
- Working with numbers, characters, strings, and operators
- Writing conditional code
- Making the code modular
- Writing loops
- Finding patterns in strings
- Working with arrays and collections
- Adopting a programming style
- Reading and writing to various locations
- Managing memory usage
- Learning about other languages
Event driven programming
Right now all our code executes as soon as the page loads our script file. It runs through it as fast as possible and finishes. But that's not good enough anymore. What I want to be able to do is start to react to some input from the user. And by input, I don't just mean prompting them for their name, but just being able to react to a whole bunch of different things. Does the user clicks somewhere on the screen, does the user resize the window, do they move the mouse? What do they do? What I want to do is start responding to events.
When they leave the form, it's another event. So these events are going on all the time. Your job as a programmer is to say, which ones do I care about? And the way that we can do this is by writing some functions and then saying, instead of just running this function when the page loads, only run it when the user clicks a button or moves their mouse. Now we don't have to keep asking, if the user is pressing their button. The operating system is going to take care of that.
The onblur event happens when I leave a field. Say I'm typing my e-mail address and I click out or hit the Tab key. This event will be called. This is one of the most common ones: myelement.onclick. Now what I've called myelement here? I'm presuming that I've created somewhere using the DOM. I've grabbed hold of it. I have a variable that represents an element on the page, whether that's an h1 or an image or a paragraph. Then I say .onclick and then I have to say, okay, that's the event, so what do I want to do? Well, the typical format is this. We actually create a function here.
And in fact here, we're putting that semicolon not because this is a function, but because the entire line is a statement: myelement.onclick = this function and whatever is inside it. So let me show you an example. I've got an identical two files that I had earlier. A very basic HTML page with a headline which has the ID of mainHeading. And that's my name that I just made up for that section. And then script.js, which I brought over from the previous one.
What this is doing right now is grabbing hold of that headline by using document.getElementById. We're using the DOM here. And right now, it's actually changing that immediately. But I've decided I don't want that to happen immediately. I want to start reacting to events. I only want to call this line if they've clicked the headline. So here's what I'm going to do. I know that the first line of code that's executed grabs hold of the headline and then I'm going to say headline.onclick.
I'm going to set up an event handler for them clicking the headline. Equals, and we're going to write an anonymous function, the word function, opening and closing parentheses, and the opening and closing curly braces. I'm going to put my semicolon in here. It would work without it, but this is the best practice. Now I'm going to grab this line of code that sets the inner HTML, the actual text value of this headline. And I'm going to paste it indented inside this anonymous function.
Change this message one little bit, to "You clicked the headline." I'll save this and I'll open up the webpage that contains it. Now we're finally having code that doesn't immediately run. We're not changing the value of the headline yet. It's going to wait until we click on it. So events are actually happening. When I move the mouse, that's considered an event. If I resize this page, that's considered multiple events. But I didn't say I cared about that.
The only one I care about is the click event on the element called headline, which means the only thing that's going to react to, it's no click events anywhere else on the page. I can all over the page right now. But if I click on the headline, we'll change it. We're now reacting to that. And this is the great thing. With very little code, we're now allowing the page to call back into our script. We've got an event-driven program that's communicating two ways, between our user interface, the visible part of our webpage, and our code behind the scenes.
A simple example here, but it's illustrating a really powerful principle of event-driven programming.
Find answers to the most frequently asked questions about Foundations of Programming: Fundamentals .
Here are the FAQs that matched your search "" :
- Q: Using TextEdit with Mac OS 10.9 Mavericks?
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.