< Return to Video

Preventing default behavior of events (Video Version)

  • 0:00 - 0:06
    Now I want to show you one more thing
    that you could do with the `event` object.
  • 0:06 - 0:10
    This webpage that I've made
    answers the eternal question:
  • 0:10 - 0:12
    "What does Oh Noes say?"
  • 0:12 - 0:17
    Or, more specifically, what would
    he sound like if you could hear him?
  • 0:17 - 0:21
    If you click on the link,
    you'll be brought to an mp3 file
  • 0:21 - 0:25
    that should play in the browser and
    answer that burning question for you.
  • 0:25 - 0:30
    However, I don't want the user
    to have to leave the webpage.
  • 0:30 - 0:33
    You should be able to hear it
    directly on the page.
  • 0:33 - 0:38
    We can do that with JavaScript,
    by playing an `` tag
  • 0:38 - 0:41
    as soon as the user clicks the link.
  • 0:41 - 0:45
    First, let's store the link
    in a variable.
  • 0:45 - 0:48
    (typing)
  • 0:56 - 0:58
    Now let's define a callback function.
  • 0:58 - 1:04
    This is going to be
    a pretty interesting one.
  • 1:04 - 1:10
    So in that callback function, we want
    to create an audio tag dynamically.
  • 1:10 - 1:12
    (typing)
  • 1:16 - 1:20
    And this is a nice new tag
    available in modern browsers.
  • 1:20 - 1:26
    And then audio.src equals--
    and we're going to set it equal to
  • 1:26 - 1:33
    what the `href` is up here--
    the audio tag is a lot like the image tag.
  • 1:33 - 1:37
    And then we're also going to say
    `audioEl.autoplay = true`,
  • 1:37 - 1:40
    that'll make it play as soon as
    we add it to the page.
  • 1:40 - 1:43
    And finally, we add it to the page.
  • 1:43 - 1:46
    And it doesn't really matter
    where I put it,
  • 1:46 - 1:48
    since we're not really visualizing it.
  • 1:48 - 1:52
    So now, when they click, it should
    create an audio, set the source,
  • 1:52 - 1:55
    set it to autoplay,
    and add it to the page.
  • 1:55 - 1:59
    Finally, we make sure that
    the event listener is called
  • 1:59 - 2:01
    when the link is clicked.
  • 2:01 - 2:04
    (typing)
  • 2:08 - 2:13
    ...and then we just pass in
    the name of the function.
  • 2:13 - 2:17
    Okay, you know the deal:
    pause the talk-through, try it out.
  • 2:17 - 2:20
    ♪ humming ♪
  • 2:20 - 2:21
    What happened?
  • 2:21 - 2:26
    For me, the sound plays--
    that low, deep grumble of Oh Noes--
  • 2:26 - 2:30
    but the link still opens up
    in a new window.
  • 2:30 - 2:33
    Ideally, once that sound played,
    the browser would no longer
  • 2:33 - 2:38
    try to navigate the user to the link
    because they've already heard it.
  • 2:38 - 2:43
    The way to do this is to tell the browser
    to cancel its default behavior.
  • 2:43 - 2:47
    You see, by default,
    when a user clicks a link
  • 2:47 - 2:50
    the browser navigates
    the user to that link.
  • 2:50 - 2:54
    But if we've overridden the way
    that the link works, in JavaScript,
  • 2:54 - 2:58
    we often don't want the browser
    to do that navigation.
  • 2:58 - 3:03
    We can tell it to stop by using
    a method on the `event` property
  • 3:03 - 3:05
    called `preventDefault()`.
  • 3:05 - 3:10
    So we need to refer to that
    event object that we get passed,
  • 3:10 - 3:16
    and then inside here, we say:
    `e.preventDefault();`
  • 3:16 - 3:19
    This should tell the browser
    to prevent the default behavior
  • 3:19 - 3:21
    associated with this event.
  • 3:21 - 3:26
    Now pause the talk-through,
    and try it again.
  • 3:26 - 3:28
    You just heard the sound, right?
  • 3:28 - 3:31
    That is a much nicer user experience.
  • 3:31 - 3:35
    And that is what is known as
    "progressive enhancement"--
  • 3:35 - 3:40
    starting the webpage as HTML with
    the default browser behavior
  • 3:40 - 3:45
    but then enhancing it with JavaScript
    to be a richer experience.
  • 3:45 - 3:48
    You'll often want to use this
    `preventDefault` when you're
  • 3:48 - 3:50
    attaching click listeners to links.
  • 3:50 - 3:54
    And you may also find yourself using it
    when you're doing form processing,
  • 3:54 - 3:57
    since the browser does some
    default behavior there as well,
  • 3:57 - 3:59
    submitting to a server.
  • 3:59 - 4:03
    The important thing is to keep
    the user experience in mind,
  • 4:03 - 4:07
    and if the user experience
    isn't optimal on your webpage,
  • 4:07 - 4:09
    figure out how to make it better.
  • 4:09 - 4:11
    We may not be able
    to teach you everything here,
  • 4:11 - 4:15
    but the Internet has
    thousands of answers for you.
Title:
Preventing default behavior of events (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:
04:17

English subtitles

Revisions