< Return to Video

The DOM (Video Version)

  • 0:00 - 0:07
    Going forward, you're going to hear
    me say a certain acronym a lot: DOM.
  • 0:07 - 0:10
    Dom de dom dom dom
  • 0:10 - 0:16
    DOM stands for
    Document Object Model,
  • 0:16 - 0:19
    and it's the way that browsers
    let us developers
  • 0:19 - 0:22
    manipulate webpages with JavaScript.
  • 0:22 - 0:27
    Whenever a browser loads a webpage,
    it parses all the HTML and CSS,
  • 0:27 - 0:30
    and converts the document into a DOM.
  • 0:30 - 0:34
    That DOM is really a
    big JavaScript object
  • 0:34 - 0:38
    that contains everything we'd ever
    want to know or change about a page,
  • 0:38 - 0:43
    like every single tag, and
    attribute and style for each tag.
  • 0:43 - 0:47
    To access the DOM on a
    webpage from JavaScript,
  • 0:47 - 0:53
    we use the global variable `document`,
    and then we can use properties
  • 0:53 - 0:58
    and call functions that
    are attached to this object here.
  • 0:58 - 1:04
    The general strategy for
    DOM manipulation is two steps.
  • 1:04 - 1:09
    Let me just make a list of them here.
  • 1:09 - 1:15
    Here we go, and then we
    have the second step.
  • 1:15 - 1:18
    Let's go through each
    of these steps now.
  • 1:18 - 1:23
    So the first step is to find the DOM node
    using an access method.
  • 1:23 - 1:29
    If all we're looking for is the
    tag, we can access that really easily
  • 1:29 - 1:34
    just by saying `document.body`.
  • 1:34 - 1:39
    And now the second step is to
    manipulate the DOM node that we found
  • 1:39 - 1:43
    by changing its attributes,
    styles, inner HTML,
  • 1:43 - 1:46
    appending new nodes to it, ... .
  • 1:46 - 1:50
    So if we just want to replace
    the contents of the whole tag,
  • 1:50 - 1:54
    then we can use the
    `innerHTML` property.
  • 1:54 - 1:59
    So `document.body.innerHTML =
    "Webpage be gone!";`
  • 1:59 - 2:02
    Ta da, I did it.
  • 2:02 - 2:06
    The browser is watching for
    changes to the document object,
  • 2:06 - 2:10
    and as soon as it sees you change the
    innerHTML of document.body,
  • 2:10 - 2:13
    it reflects it back in
    the actual document.
  • 2:13 - 2:18
    Remember, the document object
    isn't the actual webpage,
  • 2:18 - 2:24
    but the browser tries to make it reflect
    the current page as much as possible.
  • 2:24 - 2:29
    Now, there are a lot more
    ways to do step one,
  • 2:29 - 2:33
    because usually you want to find
    something besides just the body tag.
  • 2:33 - 2:38
    Maybe you want to find a tag with a
    certain ID, or all tags of a certain type--
  • 2:38 - 2:42
    That's what we'll talk about in
    the DOM access methods tutorial.
  • 2:42 - 2:45
    There's also a lot more cool stuff
    you can do in step two,
  • 2:45 - 2:49
    like changing the attributes
    or styles of the tags you find.
  • 2:49 - 2:53
    We'll talk about all that in the
    DOM modification tutorial.
  • 2:53 - 2:58
    Once you've mastered DOM access and
    manipulation, we'll move on to fun ways
  • 2:58 - 3:03
    to use it, like when responding to
    user events or making an animation.
  • 3:03 - 3:07
    Right now, it is a little silly
    that we use JavaScript to do
  • 3:07 - 3:10
    what we could have just done
    with HTML at the beginning,
  • 3:10 - 3:15
    but trust me, you'll want to master
    DOM access and DOM modification
  • 3:15 - 3:19
    so that you can make full
    interactive experiences later.
  • 3:19 - 3:25
    So, keep it up, and see you soon.
Title:
The DOM (Video Version)
Description:

This is a video recording of a talk-through, uploaded to make it easier to create subtitles.
Please watch the original interactive talk-through on Khan Academy, where you can pause and edit the code
and see the code in better resolution:
http://www.khanacademy.org/computer-programming

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

English subtitles

Revisions