Return to Video

The window object (Video Version)

  • 0:01 - 0:03
    Another fun way to use JavaScript on
  • 0:03 - 0:06
    web pages is to animate properties over
  • 0:06 - 0:09
    time. Before we can do that however, I
  • 0:09 - 0:10
    need to make sure that you know about
  • 0:10 - 0:12
    another global variable that is available
  • 0:12 - 0:15
    on every web page, the window
  • 0:15 - 0:18
    variable. Here, I will
  • 0:18 - 0:21
    console.log(window) and you can
  • 0:21 - 0:23
    pause, check out your dev tools and see
  • 0:23 - 0:25
    what's inside it.
  • 0:27 - 0:31
    Did you see? It is huge. It contains so
  • 0:31 - 0:34
    much, it is overwhelming. To make it a
  • 0:34 - 0:37
    little less overwhelming I will tell you
  • 0:37 - 0:38
    some of my favorite properties and methods
  • 0:38 - 0:41
    that you can access on it.
  • 0:41 - 0:44
    There is window.location which has
  • 0:44 - 0:46
    a bunch of information about the URL of
  • 0:46 - 0:50
    the page. Actually, let's go and just
  • 0:50 - 0:52
    write that out to the page so that you
  • 0:52 - 0:55
    don't have to keep pausing. So
  • 0:55 - 1:00
    textContent += "The URL of this page
  • 1:00 - 1:04
    is " + window.location and then that's
  • 1:04 - 1:07
    an object, so we need to reach inside and
  • 1:07 - 1:09
    say .href and there you go. That's
  • 1:09 - 1:13
    actually the URL of the iFramed web page
  • 1:13 - 1:15
    on this side.
  • 1:15 - 1:18
    Another property, is window.navigator.userAgent
  • 1:18 - 1:22
    which tells you about the browser of
  • 1:22 - 1:28
    the user. Say "The user agent is " +
  • 1:28 - 1:34
    window.navigator.userAgent;. Okay,
  • 1:34 - 1:37
    that userAgent string probably looks
  • 1:37 - 1:40
    pretty crazy to you. It's not really meant
  • 1:40 - 1:43
    to be human readable and it doesn't
  • 1:43 - 1:45
    always make a lot of sense for historical
  • 1:45 - 1:48
    reasons. So, most web developers use
  • 1:48 - 1:51
    libraries to actually understand what that
  • 1:51 - 1:53
    string means and what browser they're on,
  • 1:53 - 1:56
    and what OS and all that, because it is
  • 1:56 - 2:01
    very weird. Here is one that's not so
  • 2:01 - 2:05
    weird, window.outerWidth and window.outerHeight.
  • 2:05 - 2:13
    So let's say, "This web page is " +
  • 2:13 - 2:22
    window.outerWidth + " by " + window.outerHeight;
  • 2:22 - 2:25
    To me it says, 1280 by 715, but maybe it
  • 2:25 - 2:26
    says something different to you depending
  • 2:26 - 2:30
    on what your page looks like when you are
  • 2:30 - 2:32
    viewing this talk-through.
  • 2:32 - 2:34
    Now, let me show you something a little
  • 2:34 - 2:38
    surprising. I'm going to delete, the
  • 2:38 - 2:41
    window part of this line of code.
  • 2:43 - 2:48
    What happens is that it still works, that
  • 2:48 - 2:51
    is because window is the default
  • 2:51 - 2:53
    global variable on web pages. When the
  • 2:53 - 2:57
    browser looks for a variable that you use,
  • 2:57 - 2:59
    it will look for it on the window
  • 2:59 - 3:02
    object. And when you create a new global
  • 3:02 - 3:05
    variable, the window object will
  • 3:05 - 3:08
    actually store it as a property. That
  • 3:08 - 3:10
    means you should not declare your own
  • 3:10 - 3:13
    variables named outerWidth and outerHeight
  • 3:13 - 3:16
    because then they'll override window.outerWidth
  • 3:16 - 3:19
    and window.outHeight. Generally,
  • 3:19 - 3:21
    you should avoid global variables all
  • 3:21 - 3:23
    together since it is so easy for them to
  • 3:23 - 3:26
    collide with each other or existing variables
  • 3:26 - 3:29
    on the window. To be extra safe, you
  • 3:29 - 3:32
    can prefix your global variables. Like at
  • 3:32 - 3:36
    Khan Academy, we write KA_ in front
  • 3:36 - 3:40
    of any global variables that we have to have.
  • 3:40 - 3:42
    Okay, so that's the window object,
  • 3:42 - 3:44
    now we are going to see how you can use
  • 3:44 - 3:48
    two functions on it to make animations.
Title:
The window object (Video Version)
Description:

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

English subtitles

Revisions