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