02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School

Our first event

All the functions we worked with so far were functions that we triggered. If the Internet was linear that would do, but a computer application has a lot of events that are not linear and we don't know when they'll happened. For example, when a user clicks on something or when the page loads. In this lecture, we're going to take our first peek at a different type of function which is called a callback or an event. The first event we are going to look at is the onload event which triggers whenever the page is loaded.

Video Transcript

00:00It's almost a wrap I
00:03Know you're excited. I'm excited also because that means I'm gonna get a small break before chapter four
00:08But functions are gonna be with you from now on forever. Hopefully you really got a lot out of it
00:14This is gonna be a really short lecture. I just want to kind of tie the loose ends and talk really quickly about
00:19Onload so let's do that really quickly before we wrap up this chapter where we wrap this up and I wanted to talk about
00:26Events or callbacks just lightly
00:29So far we worked with functions and we all triggered them ourselves
00:33There's only one function that actually we didn't trigger directly ourselves, which was the jQuery's ready function
00:40Which basically called in whenever our document was ready. I
00:44Want to show you how you would do this if we weren't using jQuery at all and when you look at this
00:49You'll see that we don't have your any jQuery
00:51integration besides this one line
00:53And the way we would do it is we would go to our body and add here an on load
00:58Function and on our load we would put here a string and in there just put a JavaScript command
01:04now this would be called whenever the page was loaded and then I could just call the on ready and
01:09I would literally just trigger that specific code and if I do that and I go back to my application
01:15We'll see that my application should still work the way it did before and everything should work now
01:20There is some cross-browser compatibility issues
01:23But not really if that is the only reason you're using jQuery more or less
01:27You're covered on almost every browser now there if we look at the jQuery version of it, which is in general
01:35It's a good idea to work with jQuery because it makes sure you don't have to think about a lot of stuff that in
01:40Other situations you might need to think about so I'm just gonna make sure that I'm coming the
01:45Commenting that out so you're aware of it
01:47And when we look at it, we'll see the first the first thing with it with it is we're creating a object object
01:52Which we're gonna talk about more in chapter 4 which will be dedicated for objects
01:57Once we we wrap our document into the jQuery object
02:01Then we're calling the the ready function and we're sending into it our ready function
02:21Niski telling it hey whenever the document is ready then trigger the on ready function not before that notice how we're not
02:28Triggering our on ready, but we're sending it out as a variable and by the way every function is a variable as well
02:35So you could treat it as a variable directly or you could trigger it. So that's it
02:39We're done with chapter 3 and in chapter 4 we're going to take a much deeper explore into what objects are so see you

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!