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

JavaScript Extentions

Html5 introduced many new features among them a few changes to the JavaScript language - extensions. In this title we explore all the new extensions and see them in action in a live sample.

Video Transcript

00:01Alright, so before we could start digging deeper into the new features or the new video and the new different
00:09packages that are new both from an HTML point of view and the JavaScript point of view,
00:13I wanted us first to visit just a couple of more things
00:17regarding new features in JavaScript that are have been or newly introduced into HTML5 or have been
00:25standardized,
00:26standardized
00:27because those were the ones that were used most often.
00:31So I'm going to start us off with doing a few traces. We're not going to do a lot of changes here.
00:35We're going to mainly look into our sample video sample page that we have in your source code as well.
00:40And we're just going to tinker around with a few of these new attributes.
00:44Now one of the stuff that HTML5 and if you've been programming a little bit with JavaScript, you're probably aware of
00:51jQuery. jQuery has been basically because JavaScript mainly...
00:56Well, I have to take one step even back before we talk about jQuery.
01:00There's something that's called DOM. DOM is basically the traditional way of interacting with your
01:07elements in your HTML using JavaScript. And it's true also with XML. The traditional way of interacting with
01:14structuralized nodes has been with the DOM. Now, we're not going to dig deep into what is DOM.
01:19DOM is quite complicated and actually a lot of people found it really hard to wrap their heads around it.
01:24And it's cumbersome, and it's a lot of code, and it's not very intuitive.
01:29And that's probably one of the main reasons when HTML and XML first came to the world.
01:33They were really hard for a lot of people because it wasn't something that they were used to when they were
01:38programming in programming languages. And throughout the years a lot of people came up with all these different solutions to try to
01:44make life easier when you're interacting with these structural elements.
01:50Now if you want to learn more about the DOM and how it works,
01:52I encourage you to check out one of our titles, which is called
01:56Zero to Hero XML, where we go and we delve deep into what DOM is and kind of explain the ins and outs of
02:03what DOM is and how it actually works.
02:07jQuery came to the picture, not that far into, you know,
02:10jQuery came to solve that issue and solve a few other issues of cross compatibility between browsers.
02:16And jQuery basically was a fun, easy way to basically create a more simple way of interacting with your HTML.
02:22So even when you see here an example, you see that instead of create trying to figure out through the DOM,
02:28how do you know when the document is ready? I'm just, we're just wrapping the document inside of a jQuery
02:36wrapper, and then we just type dot ready and we could send into it a function. Now, we're not learning jQuery either,
02:41so this is a very learned long intro.
02:43But these issues have been known and these solutions such as jQuery have been known for a long time and people don't
02:50really interact with the DOM as they used to in the past. People use all these third-party tools and as part of this conversion
02:57and part of this this plan of HTML5 to try to make HTML easier and JavaScript easier,
03:04they decided to add a few stuff just to make our life easier and also to make the life easier for the different
03:10jQuerys of the future to be able to leverage these capabilities instead of needing to worry about the DOM as much.
03:17Now one of the stuff that we're introduced is a way to access information and I have to say this because if for example
03:24I wanted to get into this div class using the DOM
03:27I would have to travel through the body the div and basically treat this like a tree.
03:32Now
03:33treating it as a tree is not that useful and not that easy usually and sometimes we just want to have access to
03:39information based on a character or based on for example, we want to have all the classes that are the left class.
03:46How can we get all the different divs that have a class attribute of left?
03:50We just don't have a way in the traditional way of doing HTML.
03:54We'd have to use a third-party tool or loop through every single element and that would be really really complicated.
03:59So what I want to do next is I'm going to use this jQuery code that I have either that's not a standard, right?
04:05Because we're using an open source tool and you can take our HTML code that we have in our
04:10site and I'm basically going to type here some code just because I want to make sure that I'm only going to call it
04:15when my document is completely loaded into the browser.
04:18Now I could do it in the traditional
04:21HTML way of doing it, but I just don't want to and I feel like you probably shouldn't either.
04:25You should I encourage you to as you step into this world of HTML5 is to start using these tools such as jQuery
04:32which are just going to make your development so much easier.
04:34It's just a fantastic way of interacting with JavaScript in a much more friendly and an easy way.
04:40Alright, so we're not going to do any jQuery.
04:43We're just going to leverage this document ready of jQuery, which jQuery again is just a source code that we're adding into our application
04:50and it extends our capabilities in JavaScript not extending the language itself
04:55but extending the different features that are available to JavaScript.
04:58Okay, so that was a that was a lot of talking. So before I actually go into the sample itself,
05:04I just threw so much information. Carol, how does this sound? Does this make sense what I've just said?
05:10Yes, it does. It does it. I hear jQuery all the time. This is a perfect time to talk about it
05:16and you didn't you didn't over explain it either. So I love it.
05:19Oh great, because I was afraid that I was throwing too many too many things.
05:22So just to like say it really really condensely, we're basically saying that there's a really
05:27complicated way of doing JavaScript. No one really does it and a lot of tools were developed
05:31to try to avoid it such as jQuery.
05:33So HTML5 is here to try to throw into the picture a few new ways of interacting with
05:39JavaScript to make our lives a bit easier and save us time. So I want to see I want to show
05:44you one of those examples. So one of these examples is I could basically start off by
05:51basically, there's an extension to the to the document itself. So this is our document,
05:56which basically gives us access to our document. And I can just go to my document and I'm going
06:01to create our variable, I'm going to call it a left because I want to get all the left,
06:06all the all the classes that have a value of left inside of the class. And I'm going to
06:12type document. And I'm going to type dot get element by class name. And this is the new
06:20feature of HTML5, supposedly of JavaScript in the HTML5 world, where I could just ask for
06:26all the different elements. And it's usually it's not it's not get element but get elements,
06:32because it's a plural, there could be many, many elements that are that have a class called left.
06:37So just by writing this line of code, I'm going to get every single instance of every single
06:42element that has a left attribute at left class, really easy, very nice. And obviously,
06:48if you're working ready with jQuery, you'd probably be doing sorry, probably doing
06:53all your work in jQuery anyways. But this is just if you're not or if you're a smaller
06:57application, or if you're just not ready to jump into jQuery yet. All right, and obviously,
07:02a lot of this stuff is really more going to help the next generations of jQuery to make them
07:07less cumbersome on the system, because they're going to have the browsers give up all this
07:12information more available and the and jQuery won't need to do all these parsing on its own,
07:16because right now jQuery has to parse through the document and rebuild it to be able to get
07:21all this type of really, really cool information that you could get using these more sophisticated
07:25tools of interacting with your document. So now that we got our element, let's just check to see
07:30that it's that we're actually getting it back. So I'm going to just type a left, which is
07:34basically an array. And I'm just going to set up an alert, a JavaScript alert to just get the
07:39value that's coming back. And I'm going to toggle right into our browser, and I'm going to refresh
07:43it. As soon as the browser is finished loading, it should tell us that got us an HTML collection
07:49back. That's really good. Now, let's check how many how many elements are in this collection.
07:58And you can see the result that comes back is six. So basically, our array has six elements. So
08:03let's check what we have in our first element. And as we're checking what we have in our first
08:07element, I want to introduce another new feature, which is basically without really new, it's called
08:12inner HTML, every single element has this property of inner HTML, which basically returns its
08:17content. Now, all browsers have really been or most browsers have been supporting this. It's just
08:23never been part of the standard. So now it's been added into the standard. So browsers now and from
08:28now on in the future will all for sure implement inner HTML, because it's useful. So in this the
08:34first scenario, if I'm going to the first time that there's a left, which is actually this
08:37element, it has no content, it's a it's a node that is self contained, right? So I'll refresh
08:43it. Oh, I need to save first. That's probably a good idea. And Reese Reese reload. Okay, so we see
08:52that it's empty. And then the next step after that, let's check one that actually isn't empty,
08:56and actually, element number one isn't empty, because it has your little div here. If I'll
09:01find it. And the second one is actually a div that contains an image. So we should be getting back
09:09this image back. So let's save that and see if that really happens. Let me make sure we're at
09:14one now. And I'll refresh. And we should be getting the content and we are so we're happy.
09:22Let's take a deeper look into the HTML elements. So HTML elements now have every HTML element has
09:30actually, just as we went into our document class, and we search for to get elements by class name,
09:36we could do that also for specific elements. So if we if we get a specific element back,
09:42so let's say for example, a left inner, we could let's say, let's look at this for a second.
09:50So let's do an example here. So let's take this div, and we're just going to give it a class name
09:57of left, although we don't really want it. This is just for the sake of this example.
10:01So now our first first instance of left is going to be zero. And if we do our inner HTML,
10:06we're going to get a lot of stuff in our inner HTML. Okay, that basically is true. Alright,
10:12so the next I've instead of doing an inner HTML, what I want to do is I want to take our first
10:16element. And I'm going to do an a left. And I'm going to look for all the left classes that are
10:28inside of our first element. Alright, making it a bit more complicated. So I'm going to grab my
10:33first element that we saw now is basically this, this style, this div, and I'm going to get the
10:40exact same information. But this time around, instead of getting it from the full document,
10:43I'm getting it from that specific element that we got back. So then if we do a new alert,
10:49and we check for our first element inside of it, let's see what we get back. Okay,
10:55so we're getting empty. So let's see why we're getting empty. First of all, are we getting
10:59something back? Let's see if we're getting something back. And we are getting something
11:05about we're getting an image element. And it's the exact same image element that we were looking
11:09at earlier, that just has no content that has no inner content. So good, we just we just learned
11:15another new thing. Now, the same thing that we talked about, we talked about inner HTML,
11:20it's true for any element. Now, another convenient thing that was introduced is a way for us to
11:27access the class list, which is actually really, really cool. So what we could do if we take this
11:32as an example, I'm just going to take and I'm just going to go into our a left element here,
11:40and we're just going to get it, I'm going to pull out or let me just even alerted just
11:45right away onto the screen. And I'm going to take the element I got back. And all I really
11:49want is to get a list of all the classes. So I'm going to write class list. I'm going to go back
11:55into my browser, going to refresh it. And I'm going to see that my class list basically just
12:01has one element which is left. Now, if my element had more than that, if I if my element
12:08had more than that, so if I'm going to, I'm going to go right back up here, going to comment
12:13all this stuff out, and I'm just going to add another class, I'm going to have your left,
12:17I'm going to have your right, I'm going to have your morning. So there's three different classes
12:21that are supposedly CSS styles, just theoretical ones. If I refresh my screen, I'm going to
12:27get all of them back, which is incredibly useful. Alrighty, so let's delete a few our
12:33code that we have used over because I want to show you something really cool and a little bit
12:36of how to actually work with this class list because it comes with a few hidden gems which
12:41are really, really cool. Alright, so I deleted our what we had there so far, we're back into our
12:46document when it's ready. And what I want to do is I want to go to our document. I basically want
12:51to take our I want to find our class top, there's only one of them. So I'm going to use that class
12:56anyways, I'm going to use that search through classes, we want to practice those new features.
12:59So we're going to look for a top, and we're going to get the first in our document,
13:06we're going to get our get elements by class name, short and friendly name.
13:16And now that we got our first element, we could really then access just we really only
13:20care about that first element. So I'm just going to access that first one, because it's really
13:24only one that exists. And then I could access its class list. And just to show that it actually is
13:31coming back, we'll do your little trace. Oh, we're not in flash, we should do our alert,
13:36not a trace alert. And we're going to run our JavaScript and try to see if we could see that
13:42if we refresh our JavaScript. This is a little bit of experimenting, we'll see that we got our
13:46top element. And this is actually what we wanted to do. Alright, so the next step, what I want
13:50to do is I want to experiment with actually changing it. So I can now now that I got my
13:54list, and that list could have had many of them, I could go to that list, just put a dot and say,
14:00okay, now I want to remove the actual top class from that class from that element that I got back,
14:07I can save that. And then if we check our HTML, again, it's going to mess up the layout,
14:10because then it's not going to have all our CSS rules that we had for our top. So

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!