Setting up jQuery
jQuery helps us keep our code cross browser compatible and just makes life a lot easier when it comes to interacting with the DOM (Document Object Model). As such, let's load it up.
Video Transcript
00:02Alright, so our next step is learning how do we actually load an external script into
00:07our document.
00:09Now to do that, I'm going to go ahead and just add a new script tag.
00:14And again, instead of typing this type JavaScript, I'm going to delete that I'm going to open
00:18and close it just like I would normally do.
00:21But instead of actually putting my script inside, I'm going to type your source and
00:25then put here the direct path to the actual JavaScript file.
00:30In my case, I want to load in jQuery.
00:32So we're going to go ahead back into our browser.
00:35And I'm just going to go into the code jQuery.com where the CDN file of jQuery will be.
00:43Now notice and right now, you know, the versions are always changing.
00:46But notice right here that there's jQuery two point something and one point something.
00:51Well two points on the currently at least does not support inter Explorer nine or below
00:56nine.
00:58Well, jQuery one is fully compatible with Jake with IE nine and in our case, because
01:05we want to make sure that we have access to it, then we're going to go ahead and just
01:09click on the minified version, which is the compressed smaller file size.
01:15And we could just grab the URL itself and go back into our browser.
01:20And all we have to do is paste it in.
01:22Now, by the way, if you wanted to, you could have downloaded the JavaScript file, place
01:26it somewhere inside of your HTML area, such as creating a new JavaScript folder and putting
01:33it there.
01:34Now, the advantage of using a CDN is that this doesn't hit your server.
01:39And a bigger advantage is also that there's a lot of other sites that use jQuery.
01:44And a lot of them are using the same CDN, which basically means that if the user already
01:49loaded this file up, they don't have to load it up again.
01:53So we're gonna that's all we had to do to load up jQuery.
01:57Once we loaded in jQuery, we're ready to start actually integrating actions that are that
02:02correlate to that jQuery.
02:05So in our case, what I want to do next is I want to go right into my script tag, my
02:10other script tag, that is, and I want to continue our solution to our problem that we
02:15had earlier, that we want to only call this function when our document completes loading.
02:22Now, this is going to be a lot of script.
02:23And some of it might not be 100% legible at this stage.
02:28Don't worry about the legibility for now, and just work with it.
02:32But I'll talk about it briefly.
02:34So first of all, I'm going to call a function, which is just called dollar.
02:38And into it, I'm going to send my document.
02:43And literally what this does, it wraps our default document that exists in every HTML
02:48page, and it wraps it around the jQuery object.
02:52And now this document is going to be a jQuery document.
02:58And now that it's a jQuery document, it's going to have features that are not by
03:02default available for our default elements.
03:06And in this case, what I want to do is I'm going to click on dot and type ready.
03:10And I'm going to open and close a round bracket again.
03:14Now, what I'm basically doing here, and by the way, I put here a semicolon
03:18because we're going to talk about it.
03:20Well, let's talk about it right now.
03:22Every time you finish a command in JavaScript, it's customary to put a semicolon.
03:28That basically says I'm done with this command.
03:31So in this case, I'm basically I called a function sent into it our document.
03:37It basically converted our document into a jQuery document.
03:41And now it created a function for it.
03:43And now we have access to a function in it, which is called ready.
03:47And literally what this does is basically says, do whatever we're going to put inside
03:52of here, only when your document is ready.
03:56Now, the next step is we really need to have a function.
03:59So let's go ahead and learn how to create our own function.
04:03We've seen functions in action.
04:05We've seen functions that stood on their own.
04:07We've seen functions that belong to a document.
04:10We've seen how a function could be called and return an object and then call another
04:14function that belongs to an object.
04:16So our code is getting a lot more complex.
04:19So let's go ahead and create our function.
04:22Now the first step is we have to type the words function.
04:26Once we type in the word function, we want to give that function a name.
04:30In this case, I'm going to call it on ready, because I really want this function to be
04:35called only once and when our application is ready.
04:39Now just like with our functions, when we call them, we're going to open and close
04:42around bracket.
04:44Notice how in my editor, now my name of my function has changed color to be green.
04:49Just to help me know and see that this is the name of my function.
04:56The next step is I need to bound what is this function going to do.
05:01To be able to bound the start and end of a function, just like if in HTML, we had an
05:06opening tag and a closing tag.
05:10In JavaScript and many other programming languages, we'll open a curly bracket and
05:15close a curly bracket, basically saying everything between here and here is our function ready.
05:22And we could literally go ahead and just put that alert that we had earlier that we created
05:27earlier, and just put it right inside of that function that we've just created.
05:32And now that we know that we need to finish every line with a semicolon, so I'm going
05:35to go ahead and put a semicolon because this is one action.
05:39And I'm done with this one action, so I'm going to close it up.
05:42Even though there's multiple things going on inside of here, this is one, it all happens
05:46in one breath.
05:47And that's why it's one action.
05:50So it's one line of code.
05:52So now that I've created a function, and I've learned already how to trigger a function,
05:58let's see how do we actually send it into our ready.
06:01Now in the next video, we're going to break this down again.
06:03So don't worry, it is a little bit complicated.
06:06But what I'm going to do next is I'm going to send to my ready the on ready function.
06:11And there's a big difference between what I'm doing here versus doing it all the other
06:15times around.
06:16So we have your function.
06:18And what I'm literally doing is I'm sending that function into the ready command, I am
06:23not triggering it.
06:25Notice if I put this, this would be triggering it, this would be saying call this function.
06:29I'm not calling it instead of it, I'm sending it on.
06:31I'm taking this function that exists, and I'm saying hey, send it to the ready function.
06:36Now behind the scenes, the ready function will trigger on ready whenever the page is ready
06:41and not before that.
06:43So if I click on Save, and I go right back into my browser, and I go back into my starting
06:48page, and I click on refresh, notice that I'm going to still get that alert again.
06:53And this time, it's going to be with the actual content inside, and there's going to
06:57be completely browser independent.
07:00Now we learned a lot right now.
07:03In the next video, we're going to break down what we just learned, and see it a little
07:07bit more separately, and see, again, the function, but on its own.
07:12So let's jump right into the next lecture, it's going to be a little bit easier, we're
07:15taking kind of a step back to see how functions work, and then we're going to continue.
