This course will be retired on January 6, 2020. We recommend "HTML Basics" for up-to-date content.
You've already started watching Includes and iframes
In this video, we'll learn how to include other files into our pages, such as CSS files and JavaScript files. We'll also learn how to use iframes as windows into other pages.
-
0:00
[? Music ?] [Treehouse]
-
0:08
So far, we've learned about images, objects, and embeds.
-
0:12
Now let's take a look at a few more elements
-
0:14
that will help you include content into your web pages.
-
0:19
Let's type out our first link element.
-
0:23
We're going to put our link element inside of the head
-
0:27
rather than inside of the body,
-
0:30
and first, I'd like to emphasize that this is
-
0:33
not the same as an anchor tag.
-
0:37
This actually isn't going to create any kind of link
-
0:40
that the user can click on, so you want to put this
-
0:43
in the head of your document, not in the body.
-
0:46
The link tag includes other resources into the page.
-
0:50
So, I'm going to type some attributes out here,
-
0:53
and then I'll explain them.
-
0:55
First is the rel attribute, or relative attribute,
-
0:59
and we're going to say this is a stylesheet.
-
1:02
The type will be "text/css,"
-
1:06
and then we'll create an href, or a hyperlink reference
-
1:10
to "application.css."
-
1:14
Now, unfortunately, the link tag isn't very useful to us yet
-
1:18
because we haven't learned about how to use CSS documents.
-
1:23
As you may have guessed, however, this will become more meaningful
-
1:26
when we combine HTML with CSS.
-
1:30
If we were to refresh the browser,
-
1:33
you wouldn't actually see anything come up in the page
-
1:36
because, of course, anything that is rendered in the browser
-
1:39
comes up in the body tag here.
-
1:42
Next up is the script element, which is actually pretty similar
-
1:46
to the link element except it's used for including JavaScript.
-
1:49
JavaScript is something you want to learn later on,
-
1:54
but let's just see what this might look like in a text editor.
-
1:57
We'll type out our script tag,
-
2:01
and we'll use the the type "text/javascript,"
-
2:08
and the source attribute we'll set to a folder we'll call
-
2:13
"javascripts," and then we'll say "app.js" is our JavaScript file.
-
2:19
And then for the script tag, we actually need a closing tag here,
-
2:24
so just like the link element,
-
2:27
we also have a type attribute here
-
2:30
and a source attribute here, which is pretty much the same
-
2:33
as the href attribute.
-
2:36
Now, why do we have a closing tag?
-
2:38
Well, we can actually put things between these 2 script tags
-
2:43
rather than including them using an external file.
-
2:46
Now, it is best practice to include your JavaScript with an external file,
-
2:50
but in a pinch, you can go ahead and put it on the page.
-
2:55
So, we're actually going to write our first bit of JavaScript
-
2:59
before we've even learned any JavaScript.
-
3:02
So, we'll just type "alert():"
-
3:09
and in between the parentheses we'll put single quotes,
-
3:14
and inside of our single quotes we'll say 'Hello World!'
-
3:21
Now, anything you put inside of the quotes,
-
3:24
including the exclamation point, will actually appear
-
3:27
in the browser as an alert box.
-
3:30
So, let's go ahead and switch back and refresh the page.
-
3:36
And off screen here, we have this alert box appear.
-
3:40
We'll just go ahead and click "Okay" to dismiss it.
-
3:43
Like I said, as you can see, we're using script tags
-
3:47
to wrap our JavaScript rather than including an external file.
-
3:53
We'll learn more about JavaScript later on.
-
3:56
Next up is the iFrame element.
-
3:59
Now, in some very specific instances,
-
4:02
you'll need to smash 2 web pages together.
-
4:05
Basically, an iFrame allows you to load 1 page
-
4:08
into another page, which is sort of inside of a box.
-
4:12
You can almost think of it as a window sitting on the page
-
4:15
that opens up into another page.
-
4:18
So, let's try an example.
-
4:20
We need to create actually a 2nd web page
-
4:23
in order for this to work, so we'll create a page called
-
4:27
"another_page.html."
-
4:31
And we want to make sure that this is in the same folder
-
4:36
as our index.html,
-
4:39
and we'll just delete what we have here
-
4:42
in index because we don't actually need it,
-
4:45
and in our index file, we're going to create an iFrame.
-
4:49
Let's go ahead and type this out.
-
4:52
We'll say "iframe."
-
4:55
We'll set the width to 300.
-
4:59
We'll set the height attribute to 300 as well
-
5:02
so we have a square box,
-
5:04
and then we need to set the source attribute to
-
5:08
the HTML document that we just created,
-
5:10
which is called "another_page.html."
-
5:14
And once again, make sure that this is in the same folder
-
5:16
as the index.html file, otherwise it won't work.
-
5:21
If you do want to put the file into a separate folder,
-
5:25
you'll need to set the path on this source attribute appropriately.
-
5:31
So, we'll go ahead and put in our closing iFrame here,
-
5:36
and then over on another_page.html,
-
5:39
we'll copy everything we have and paste it in there,
-
5:43
and we'll replace the iFrame with just some text.
-
5:47
We'll say "This is some text that will appear inside the iframe."
-
5:56
And we should probably wrap that in a paragraph element,
-
6:00
just to be semantic.
-
6:02
We'll save that out, and when we switch over to the browser and refresh the page,
-
6:08
you can see that although we're actually in the root of objects,
-
6:11
which would make the index.html file come up,
-
6:15
we're still seeing an iFrame
-
6:18
open up to another_page.html.
-
6:22
So, just to show you how this is working,
-
6:24
let's go ahead and modify the URLs here.
-
6:27
We'll actually go to index.html, which is the page that we're already on,
-
6:32
and of course, since we're already on that page, nothing should change.
-
6:36
But we'll change this to another_page.html,
-
6:41
and now you can see we're actually just seeing
-
6:44
that text without the box around it.
-
6:47
That's because we're not on index.html.
-
6:50
And just to demonstrate that, let's go ahead and add some paragraph text
-
6:56
just above the iFrame.
-
6:58
I'll say "Here is index.html,"
-
7:03
switch back and refresh the page.
-
7:07
Oops, it looks like the page 404.
-
7:10
We have another page here.
-
7:12
Let's go ahead and check in our text editor, and yep,
-
7:15
it looks like we actually misspelled "another."
-
7:18
Let's go ahead and just rename that
-
7:23
and switch back and refresh, and there you can see
-
7:27
our text is now appearing, but we're not seeing text
-
7:30
that's in index.html.
-
7:33
If we just go back to the root and bring up index.html,
-
7:38
you can see there's the text we put in that file,
-
7:41
and here is the text that's in another_page.html.
-
7:46
There is 1 last thing that we should cover
-
7:48
before we learn more about HTML, and that's comments.
-
7:52
This isn't really a part of objects,
-
7:54
but we should learn about them before moving any further.
-
7:57
Comments allow you to insert notes about your code
-
8:00
that aren't actually displayed to the user.
-
8:03
However, if the user looks at the source code of your HTML page,
-
8:07
then they will be able to see your comments,
-
8:10
so you don't want to put any sensitive information in them.
-
8:14
We can view the source in Chrome on Mac OS X
-
8:18
by hitting Command, Alt, U,
-
8:20
and that will bring up the source code.
-
8:23
Alternatively, if you're using another browser
-
8:26
or if you don't want to use those hot keys or if you're not on a Mac,
-
8:30
you can usually just go to the View menu, and in most browsers
-
8:34
you'll be allowed to view source.
-
8:36
Alternatively, you can just right-click on the page
-
8:39
and hit "View Page Source."
-
8:43
Now that we have the page source up,
-
8:45
we can actually see all the HTML that we typed out.
-
8:49
This isn't actually a secret to the user.
-
8:52
Let's try adding in some comments.
-
8:55
We'll switch back over to our text editor,
-
8:57
and while comments aren't always necessary in simple HTML code,
-
9:02
in more complex cases like with CSS or JavaScript,
-
9:05
you'll probably want to include more comments
-
9:08
so that you don't lose track of what's going on.
-
9:11
Comments are particularly useful when something goes wrong in your code
-
9:15
because you can debug it more easily,
-
9:17
especially when you have to reopen that code a long time
-
9:21
after you originally wrote it.
-
9:23
Let's go ahead and add in our HTML comment here.
-
9:26
Just above our paragraph, we'll open up some angle brackets
-
9:31
and type an exclamation point, 2 dashes, a space,
-
9:36
followed by 2 more dashes.
-
9:39
We have 2 spaces here,
-
9:42
and in the middle of those 2 spaces,
-
9:46
we want to type our HTML comment.
-
9:48
We'll say "This is an HTML comment."
-
9:53
"Below is a paragraph."
-
9:57
We'll save that out, switch back to the browser and refresh.
-
10:01
And I think we actually did this in another page,
-
10:04
so we can just type in "another_page.html."
-
10:10
And when we refresh, you can see that we now have
-
10:13
"This is an HTML comment. Below is a paragraph."
-
10:17
But if we actually go to that page in the browser,
-
10:22
you can see that we don't have that HTML comment appearing.
-
10:26
You can only see it when you view the page source.
-
10:31
That about wraps things up for the group of HTML elements
-
10:35
that are classified as includes or objects.
-
10:39
[Treehouse]
You need to sign up for Treehouse in order to download course files.
Sign up