Return to Video

Putting JS In a webpage (Video Version)

  • 0:01 - 0:07
    A basic webpage, like this one,
    is made up of HTML tags, like all these.
  • 0:07 - 0:12
    Now, when we want to style a webpage,
    how do we bring CSS into it?
  • 0:12 - 0:14
    We add a `` tag.
  • 0:14 - 0:17
    And the browser knows
    when it sees a style tag
  • 0:17 - 0:20
    to use its CSS engine
    to process that tag.
  • 0:20 - 0:23
    We usually put the `` tag
    in the ``,
  • 0:23 - 0:27
    because we want to make sure that
    the browser processes the styles
  • 0:27 - 0:31
    before it renders the HTML tags.
  • 0:31 - 0:35
    Otherwise, if we put ``
    at the bottom here,
  • 0:35 - 0:40
    then we could have an FOUC:
    a flash of unstyled content,
  • 0:40 - 0:45
    and people would see
    our wepage naked-- gross!
  • 0:45 - 0:48
    Let's bring it back here where it belongs.
  • 0:48 - 0:52
    Okay, so now this webpage has style.
  • 0:52 - 0:58
    How do we bring JavaScript into a webpage
    when we want to add interactivity?
  • 0:58 - 1:01
    For that, we add a `` tag.
  • 1:01 - 1:04
    And the best place to put a `` tag
  • 1:04 - 1:11
    is actually at the very bottom of the page
    right before your end `` tag.
  • 1:11 - 1:15
    I've put it there, and
    I'll explain why it's there in a bit.
  • 1:15 - 1:20
    Now what can I put
    inside this `` tag?
  • 1:20 - 1:25
    Hmm, we can put any valid JavaScript in it,
    like `var four = 2 +2;`.
  • 1:25 - 1:30
    But that's not terribly exciting, because
    nothing's happening on our webpage.
  • 1:30 - 1:33
    And if you're on Khan Academy,
    you probably already knew
  • 1:33 - 1:36
    that four equals two plus two.
  • 1:36 - 1:39
    So one thing I can do
    to check that it works,
  • 1:39 - 1:42
    is to write this line of code here.
  • 1:42 - 1:45
    Okay, you don't see anything, right?
  • 1:45 - 1:49
    And maybe you've never seen
    this function before.
  • 1:49 - 1:53
    This function, `console.log`,
    is a special one we can use
  • 1:53 - 1:56
    in many JavaScript environments,
    including browsers.
  • 1:56 - 2:00
    And it will write out things
    to the JavaScript console.
  • 2:00 - 2:04
    You can find that console
    in your browser
  • 2:04 - 2:09
    by pressing Command-Option-I,
    or Control-Option-I,
  • 2:09 - 2:14
    or right-clicking anywhere on the page,
    and selecting "Inspect element".
  • 2:14 - 2:19
    Pause the talkthrough now,
    and try to bring up your dev console
  • 2:19 - 2:22
    to see that message.
  • 2:22 - 2:24
    So, did you see it? Great!
  • 2:24 - 2:27
    You can close the console now
    if you'd like,
  • 2:27 - 2:29
    as it can take up a lot of room.
  • 2:29 - 2:33
    It can also get distracting since
    it'll show you every error as I'm typing.
  • 2:33 - 2:36
    It is a great tool for debugging, though.
  • 2:36 - 2:38
    So definitely file it away
    in your toolbox.
  • 2:38 - 2:42
    Now let me actually do something
    to the page with JavaScript,
  • 2:42 - 2:47
    using a line of code that
    will not make a lot of sense yet.
  • 2:57 - 2:59
    See what happened?
  • 2:59 - 3:03
    Our webpage disappeared, and was replaced
    with our "leet hacker" message.
  • 3:03 - 3:08
    We'll go into more details about
    how this line of code actually works.
  • 3:08 - 3:12
    But basically it found the `` tag,
    and replaced the contents.
  • 3:12 - 3:17
    Now what would happen if I copied
    and pasted this `` tag,
  • 3:17 - 3:20
    and stuck it up in the ``
    with the `` tag?
  • 3:20 - 3:23
    It doesn't work-- why not?
  • 3:23 - 3:27
    Because the webpage evaluates
    the `` tag
  • 3:27 - 3:29
    before it sees the `` tag.
  • 3:29 - 3:35
    And it says, "Uh-oh, I haven't even seen
    `document.body` yet,
  • 3:35 - 3:38
    "And you're trying to manipulate it!
    That can't happen."
  • 3:38 - 3:44
    That's why we have to put our
    `` tag at the bottom of the page.
  • 3:44 - 3:47
    So that the webpage sees
    the `` tag first,
  • 3:47 - 3:51
    sees everything in it,
    and then we start doing stuff to it.
  • 3:51 - 3:55
    We want to make sure that
    the webpage exists first.
  • 3:55 - 3:57
    And that's different from CSS:
  • 3:57 - 4:00
    We want to put our `` tag
    at the beginning,
  • 4:00 - 4:03
    because the CSS parser is fine
    with applying styles
  • 4:03 - 4:04
    to things that don't exist yet.
  • 4:04 - 4:07
    It'll just apply them once they happen.
  • 4:07 - 4:10
    But JavaScript DOM is not fine with that.
  • 4:10 - 4:13
    So make sure it goes at the bottom here.
  • 4:13 - 4:16
    Try adding the `` tag
    in the next challenge,
  • 4:16 - 4:19
    making sure you put it at the bottom,
  • 4:19 - 4:24
    and then I promise I will explain
    much more about this line right here.
Title:
Putting JS In a webpage (Video Version)
Description:

more » « less
Video Language:
English
Duration:
04:26

English subtitles

Revisions