Starting to code on page load complete
Wouldn't it be nice if you could only start your JavaScript code after the page completed loading? I hope you, said yes as that's exactly the topic of this lecture.
Video Transcript
00:00Welcome back. So in the last few lectures, we've seen functions. We've seen them as standalone functions
00:06We've seen them also as functions that belong to an object and we said those are called methods
00:12But it's basically functions that belong to an object
00:16Now don't worry about what an object is don't worry about the too much technicalities
00:21Besides seeing the difference between the two where alert is global. It's a function that could
00:27Is not connected to something while this function is connected to the document
00:31So it's basically almost like we're asking the document to do something for us
00:36So I'm going to comment this out again using that
00:40slash slash
00:42And I want to show you how do we actually get the actual script to load up only once our page has loaded
00:50now the most simple way of doing that is going into our body and
00:54Pressing on space and writing there on load
00:58Putting there an equal and putting there a value now the value that we're gonna put in the on load is basically JavaScript
01:05So I could go ahead and just type your alerts
01:08All right, literally, you know what? Let's go ahead and just copy this whole thing
01:14So I'm just gonna copy that script that we had earlier paste it right inside save it go back into my browser
01:20Click on refreshing. We're gonna see that our object work is gonna come back and it's gonna come back after the page has completed loading
01:28So so that is one way of doing it now the problem with loading the page in this way
01:34Is that it opens up the door to?
01:37cross compatibility issues and it also
01:41Makes our document a lot less
01:44Legible in many ways if we have a lot of things in our page and we want to approach them
01:48It's it's a lot nicer in programming if we could split between our JavaScript and put it in one place and our HTML and put it
01:56In another place and for example, you might want to use the same script for a few pages
02:01So you don't want to need to override and and write too much into a page
02:07Now I'm gonna comment this out in the HTML commenting because we're still inside of an HTML area
02:13So that'll that will comment out our on load area here in HTML and let me just save that out
02:19So we so this won't be inside
02:22now I
02:23Want to talk about the next phase and the next phase is how do we actually get it to load?
02:30without it actually
02:32Without actually having issues of worrying about cross compatibility without needing to worry about having our code
02:39Necessarily baked inside our actual content. Ideally. I want to tell my script up here that load up whenever you're ready
02:47Now I could do this with script
02:49But then every browser has its own hiccups and has its own issues which could become very hard
02:56So instead of solving this problem right away
02:59What I want to do is take a break from this topic for a second
03:03And we're going to talk about how do we load external files and with that?
03:07How do we load in jQuery because we're going to use jQuery to save us some time and load in?
03:14Basically these cross compatibility issues fading them out so we don't have to worry about cross compatibility issues
03:21So in the next video what we're gonna do is we're gonna see how do we actually load in an external JavaScript file?
03:27And in this case, we're gonna be loading up jQuery. So we'll see you in the next lecture when we do that
03:33you
