Latest / CyberSecurity Summary / JavaScript for Web Developers: Understanding the Basics
Transcript
- 0:00Speaker 1: Welcome to the deep dive. Today, we're taking a really
- 0:03Speaker 1: focused look at JavaScript, specifically for web developers. We're pulling
- 0:08Speaker 1: everything from this one pretty comprehensive book. But hey, even
- 0:12Speaker 1: if you're not a coder, stick around. Yeah, definitely understanding
- 0:16Speaker 1: JavaScript basics. It's kind of like getting a peek under
- 0:18Speaker 1: the hood of you know, all those interactive websites who
- 0:21Speaker 1: use every single day exactly.
- 0:22Speaker 2: Think of it as uncovering the essential mechanics our source.
- 0:28Speaker 2: It's aimed at web developers, but the core ideas right,
- 0:32Speaker 2: and you know some of the let's say, surprising ways
- 0:35Speaker 2: JavaScript works. That's interesting for anyone curious about webtech.
- 0:39Speaker 1: So our mission today is well, pretty straightforward but important. Yeah,
- 0:43Speaker 1: pull out the key building blocks of JavaScript, demystify how
- 0:46Speaker 1: it makes websites actually do things, and hopefully you'll have
- 0:49Speaker 1: a few of those aha.
- 0:50Speaker 2: Moments we should. The book covers a lot, from basic
- 0:53Speaker 2: syntax writing the code, to making page elements react, even
- 0:57Speaker 2: server communication loads to cover.
- 1:00Speaker 1: Let's just dive right in chapter one the basics.
- 1:03Speaker 2: Okay, so first off, the book really emphasizes using the
- 1:08Speaker 2: browser's developer tools, specifically the console. Ah.
- 1:12Speaker 3: The console.
- 1:13Speaker 1: Yeah, it can look a bit intimidating candidate, it.
- 1:16Speaker 2: Can, but it's actually a fantastic place to just try
- 1:18Speaker 2: stuff out, like a sandbox.
- 1:20Speaker 1: Okay. The book gives an example, right like three plus five.
- 1:22Speaker 2: Yep, type three plus five hit ender and boom eight.
- 1:25Speaker 2: Makes perfect sense.
- 1:26Speaker 1: But then it shows console dot log hello, and that
- 1:29Speaker 1: gives you hello, but then also undefined. Okay, what's up
- 1:34Speaker 1: with the undefined there? Why does that show up?
- 1:36Speaker 2: Well, it's about what console dot log is designed to do.
- 1:39Speaker 2: Its main job is to display something like the text hello, okay,
- 1:42Speaker 2: and it does that, but the function itself doesn't really
- 1:45Speaker 2: return a useful value back to the program in the
- 1:47Speaker 2: way three plus five returns eight.
- 1:49Speaker 1: Ah.
- 1:50Speaker 3: I see.
- 1:50Speaker 2: So in JavaScript, if a function doesn't explicitly say return
- 1:54Speaker 2: this value, it just automatically returns undefined.
- 1:57Speaker 1: So console dot log does its job displaying hello, but
- 2:00Speaker 1: the result of running the function is just nothing useful,
- 2:03Speaker 1: hence undefined.
- 2:04Speaker 2: Exactly like think of a printer at printer document, right,
- 2:07Speaker 2: but the act of printing itself doesn't like create a
- 2:11Speaker 2: new document as its output.
- 2:12Speaker 1: It just performs the action that printer analogy actually helps
- 2:15Speaker 1: a lot.
- 2:15Speaker 2: Okay, action versus result.
- 2:17Speaker 1: Got it, and the book also sets the stage, you know,
- 2:20Speaker 1: explaining that JavaScript is basically just text plain text files, right, and.
- 2:24Speaker 2: The browser reads those files and interprets the instructions.
- 2:29Speaker 1: Yeah, it executes the code. And these files are made
- 2:31Speaker 1: up of statements individual instruction.
- 2:33Speaker 2: Like console do log Hello, that's one statement precisely.
- 2:38Speaker 1: And you can group statements together in blocks using curly braces,
- 2:41Speaker 1: sometimes optional for single lines, but the book uses them
- 2:44Speaker 1: consistently for clarity.
- 2:46Speaker 2: Good practice probably, And semicolons.
- 2:49Speaker 1: At the end of statements. The book says they're often
- 2:51Speaker 1: optional if you use line breaks.
- 2:53Speaker 2: Yeah. JavaScript tries to be clever and insert them. Sometimes
- 2:56Speaker 2: it's called automatic semicolon.
- 2:58Speaker 1: Insertion, but the book uses them anyway.
- 3:00Speaker 2: It does for consistency and to avoid you know, potential
- 3:04Speaker 2: weird edge cases where the automatic insertion might guess wrong.
- 3:08Speaker 2: It's generally seen as safer.
- 3:09Speaker 1: Okay, good to know. Then variables super fundamental.
- 3:14Speaker 2: Absolutely. Think of them as labeled boxes storage locations in memory.
- 3:17Speaker 1: And you use the equals sign the assignment operator to
- 3:20Speaker 1: put something in the box.
- 3:22Speaker 2: Right, the book uses a three, then plus one, then
- 3:25Speaker 2: console dot log.
- 3:26Speaker 1: Which shows four, so you put three in a set.
- 3:28Speaker 1: Then you take what's an eye, which is three, add one,
- 3:31Speaker 1: and put the result four back into exactly that.
- 3:34Speaker 2: A plus one looks a bit strange mathematically, but it's
- 3:37Speaker 2: standard in programming. Update the variable based on its current value.
- 3:41Speaker 1: And for those common plus one and opus one operations,
- 3:44Speaker 1: there are shortcuts.
- 3:45Speaker 2: Yep, the increment plus plus and decrement operators just shorthand
- 3:49Speaker 2: for adding or subtracting one very common.
- 3:52Speaker 1: And there are other assignment shortcuts too, like plus.
- 3:54Speaker 2: Rate plus a ten is just a shorter way to
- 3:56Speaker 2: write A plus.
- 3:57Speaker 1: Ten makes sense, like the example A five plus equals
- 4:00Speaker 1: ten results in fifteen. Keeps the code a bit tidier.
- 4:04Speaker 2: It does, especially when things get more complex, saves it.
- 4:06Speaker 2: A typing makes the intent clear once.
- 4:09Speaker 1: You know them now, something that can trip people up.
- 4:11Speaker 3: Case sensitivity, oh yeah, hugely important to JavaScript.
- 4:14Speaker 1: The book stresses that thing with a lowercase T is
- 4:18Speaker 1: totally different from thing with an uppercase.
- 4:20Speaker 2: T, completely different variables as far as JavaScript is concerned,
- 4:24Speaker 2: and that difference it can lead to some really really
- 4:27Speaker 2: hard to find bugs.
- 4:28Speaker 1: Because you defined it one way and try to use
- 4:29Speaker 1: it another and suddenly it's undefined or not what you
- 4:32Speaker 1: expect exactly.
- 4:33Speaker 2: You've got to be precise with spelling and capitalization like
- 4:36Speaker 2: library call numbers.
- 4:38Speaker 1: Okay, how do we actually declare these variables? The book
- 4:41Speaker 1: introduces var, let, and const.
- 4:45Speaker 2: Right. So var is the older way if you just
- 4:47Speaker 2: say var my variable. Its initial value is undefined.
- 4:52Speaker 1: Until you assign something to it, yep.
- 4:54Speaker 2: Whereas let and const are newer constants or values you
- 4:57Speaker 2: don't intend to change later.
- 4:59Speaker 1: Constants you have to give a const of value when
- 5:01Speaker 1: you declare it, right. You can't just say const x correct.
- 5:04Speaker 2: Yeah, you can't reassign it later either. Lit is for
- 5:07Speaker 2: variables that you do expect to change.
- 5:08Speaker 1: And var it has some quirks, something about hoisting.
- 5:12Speaker 2: Yeah. Var declarations get sort of mentally moved to the
- 5:15Speaker 2: top of their scope by the JavaScript engine. It can
- 5:19Speaker 2: sometimes cause confusing behavior if you're not aware of it.
- 5:22Speaker 1: Whereas let and const don't do that.
- 5:24Speaker 3: They have what's called block scope exactly.
- 5:26Speaker 2: They're generally considered safer and lead to fewer surprises. The
- 5:30Speaker 2: book also warns about just using variables without declaring them
- 5:33Speaker 2: at all.
- 5:34Speaker 1: Oh right, in non strict mode.
- 5:37Speaker 2: Yeah, JavaScript might just create a global variable for you,
- 5:40Speaker 2: which is usually a bad idea can cause conflicts and bugs.
- 5:44Speaker 1: So always declare your variables, use let or const preferably.
- 5:48Speaker 2: That's the modern best practice.
- 5:49Speaker 1: Yes, okay, to keep code readable, we use.
- 5:52Speaker 2: Comments crucial for single line comments and followed by for
- 5:56Speaker 2: multiline comments. Yeah. Great for explaining why you did something
- 5:59Speaker 2: or for temporarily disabling code during testing. Just remember you
- 6:03Speaker 2: can't nest the multi lined comments.
- 6:05Speaker 3: Good tip.
- 6:06Speaker 1: And finally for this section, strict mode.
- 6:08Speaker 2: Ah, yes, you strict You put that string is the
- 6:12Speaker 2: very first thing in your script or the top of
- 6:14Speaker 2: a function.
- 6:15Speaker 1: And what does it do? Make JavaScript stricter?
- 6:17Speaker 2: Pretty much? It tells the browser, hey, be less forgiving
- 6:20Speaker 2: about errors and enforce stricter rules.
- 6:23Speaker 1: So it catches potential problems that regular JavaScript might just.
- 6:26Speaker 2: Ignore exactly, things that are considered bad practices or ambiguous code.
- 6:31Speaker 2: Strict mode often turns those into actual errors, making them
- 6:34Speaker 2: easier to find and fix. Highly recommended for new code.
- 6:38Speaker 1: Right helps you write cleaner, more reliable code from the start.
- 6:41Speaker 2: Okay, Moving on from the basics of writing code, the
- 6:44Speaker 2: book gets into the actual types of data. JavaScript works
- 6:48Speaker 2: with data types. Right starts with numbers, and the key
- 6:50Speaker 2: thing here is that JavaScript doesn't really distinguish between whole numbers, integers,
- 6:54Speaker 2: and numbers with decimal points floating point number. It's calls
- 6:57Speaker 2: of all number yep one single number type, which leads
- 7:00Speaker 2: us to a classic.
- 7:02Speaker 1: Issue ah the point one plus point two problem.
- 7:04Speaker 2: The very same console dot log zero zero zero zero
- 7:09Speaker 2: zero zero zero zero doesn't give you point.
- 7:10Speaker 1: Three, it gives you point three zero zero zero zero
- 7:13Speaker 1: zero zero zero zero zero zero zero zero zero zero
- 7:16Speaker 1: zero zero zero zero zero zero zero zero zero zero
- 7:18Speaker 1: zero zero zero zero zero zero zero four are something
- 7:21Speaker 1: ridiculously long.
- 7:22Speaker 2: Like that exactly. It's because computers use binary base two internally,
- 7:27Speaker 2: and just like thirteens that are repeating decimal in our
- 7:29Speaker 2: base ten system, some simple base ten fractions don't have
- 7:32Speaker 2: an exact finite representation in binary.
- 7:35Speaker 1: So tiny rounding errors creep. In the book mentions this
- 7:38Speaker 1: isn't just a JavaScript thing, right.
- 7:39Speaker 2: No, it happens in many languages. JavaScript just doesn't hide
- 7:42Speaker 2: it as much. It's something to be aware of, especially
- 7:45Speaker 2: if you're doing precise financial calculations for example good point.
- 7:48Speaker 1: So, besides that quirk, you can write numbers normally with
- 7:51Speaker 1: decimals or using exponential notation like one point two three
- 7:55Speaker 1: E four four twelve fo hundred.
- 7:56Speaker 2: Right and briefly it mentions binary notation with zero bo okay.
- 8:00Speaker 1: Then operators for numbers we saw, but the arithmetic ones.
- 8:04Speaker 2: The usual suspects plus for edition, subtraction, multiplication, division.
- 8:08Speaker 1: And the crucial part operator precedence the order they happen in.
- 8:12Speaker 2: Yes, just like math class parentheses first, then multiplication and division,
- 8:17Speaker 2: then addition and subtraction.
- 8:19Speaker 1: And if they have the same precedence like Warren.
- 8:21Speaker 2: Evaluated left to right. The books example twelve to two
- 8:24Speaker 2: to three really shows this right.
- 8:26Speaker 1: It's twelve two which is six, then sixty three which
- 8:29Speaker 1: is eighteen, not twelve two three exactly.
- 8:32Speaker 2: Understanding precedents avoids surprises.
- 8:34Speaker 1: And there's the remainder operatorcent shit, the.
- 8:36Speaker 2: Percent sign one hundred perc seven gives you two because
- 8:39Speaker 2: one hundred divided by seven is fourteen with the remainder
- 8:42Speaker 2: of two.
- 8:43Speaker 1: And the book says it's useful for things like cyclic patterns.
- 8:46Speaker 2: Yeah, figuring out days of the week, or maybe alternating
- 8:49Speaker 2: styles in a list, anything where you need to know
- 8:51Speaker 2: the remainder after division, or just checking if a number
- 8:53Speaker 2: is even or odd number percent two okay.
- 8:57Speaker 1: Next, data type strings text.
- 9:01Speaker 2: Sequences of characters. You enclose them in either single quotes
- 9:04Speaker 2: or double quotes.
- 9:05Speaker 1: The book uses single quotes mostly, but the console often
- 9:08Speaker 1: shows double yeah.
- 9:09Speaker 2: Either's fine. Just be consistent within a project, and importantly
- 9:12Speaker 2: use the straight quotes, not the curly typographic ones. Some
- 9:15Speaker 2: word processors like to.
- 9:17Speaker 1: Use gotcha, and a key property of strings immutable.
- 9:20Speaker 2: Yes. Immutable means once a string is created, you cannot
- 9:23Speaker 2: change its contents directly.
- 9:25Speaker 1: So I five Hello and I want jello. I can't
- 9:27Speaker 1: just swap the H.
- 9:27Speaker 3: For a J.
- 9:28Speaker 2: Nope. Any operation that seems to modify a string actually
- 9:32Speaker 2: creates a brand new string in memory with the changes.
- 9:35Speaker 2: The original Hello is untouched.
- 9:36Speaker 1: Okay. Good to know, especially for performance. If you're doing
- 9:39Speaker 1: lots of string changes.
- 9:41Speaker 2: It can be yeah. The book also mentions escape characters
- 9:44Speaker 2: using the backslash.
- 9:45Speaker 1: Right, like if you want a single quote inside a
- 9:48Speaker 1: single quoted string.
- 9:49Speaker 2: Don't exactly or end for a newline character T for
- 9:52Speaker 2: a tab, et cetera.
- 9:53Speaker 1: And joining strings together concatenation.
- 9:55Speaker 2: Just use the plus operator. Hello plus plus world gives
- 9:58Speaker 2: you Hello World.
- 10:00Speaker 1: Careful if you mix strings and numbers, very careful.
- 10:02Speaker 2: Five plus ten is fifteen, but five plus ten is
- 10:05Speaker 2: five to ten. JavaScript sees the string and decides to
- 10:09Speaker 2: do concatenation converting the number ten to the string ten.
- 10:12Speaker 3: Ah.
- 10:12Speaker 1: Okay, potential, gotcha.
- 10:13Speaker 2: There definitely strings also have built in properties and methods.
- 10:16Speaker 2: The book mentions.
- 10:17Speaker 1: Length absdef dot length would be six easy.
- 10:20Speaker 2: Enough, yep. And then parcent and pars float for converting
- 10:24Speaker 2: strings to numbers.
- 10:25Speaker 1: Right, so parsent one two three gives the number one
- 10:28Speaker 1: twenty three? What about parsent one towiny three guys.
- 10:31Speaker 2: Still gives one twenty three? It reads the numbers from
- 10:34Speaker 2: the start until it hits a non numeric character.
- 10:36Speaker 1: And parcent reygo ze one two three.
- 10:39Speaker 2: That would return nan, not a number, because it couldn't
- 10:43Speaker 2: find any valid number at the very beginning.
- 10:45Speaker 1: Okay, so nan is this special value indicating an invalid number?
- 10:49Speaker 2: Result exactly, and there's a function is nan to specifically
- 10:53Speaker 2: check if value is nan useful.
- 10:55Speaker 1: The book also quickly mentions booleans.
- 10:58Speaker 2: True and false the logical values fundamental for decisions in
- 11:02Speaker 2: statements loops.
- 11:04Speaker 1: Though not much detail in this chapter. And then null
- 11:07Speaker 1: and undefined.
- 11:07Speaker 2: Right two ways of representing nothing. Null is usually an
- 11:10Speaker 2: intentional assignment of no value, like saying this variable should
- 11:14Speaker 2: currently hold nothing.
- 11:15Speaker 1: Whereas undefined often means it hasn't been given a value.
- 11:18Speaker 2: Yet, often yes, or it's the default return value from
- 11:21Speaker 2: a function that doesn't explicitly return anything crucially undefined itself
- 11:25Speaker 2: isn't an error.
- 11:26Speaker 1: State, just means no value assigned.
- 11:28Speaker 3: Okay, all right?
- 11:28Speaker 2: That leads nicely into functions.
- 11:29Speaker 1: Reusable blocks of code. The book gives an example of
- 11:32Speaker 1: function twice in return number two.
- 11:33Speaker 2: Yeah, so twice is the function name. Number inside the
- 11:37Speaker 2: parentheses is a parameter, a placeholder for the input.
- 11:41Speaker 1: And when you call it like twice five, the five
- 11:45Speaker 1: is the argument passed in exactly, The.
- 11:47Speaker 2: Value five gets assigned to the number parameter inside the
- 11:50Speaker 2: function for that specific call, and the return keyword that
- 11:53Speaker 2: specifies the value the function should output or send back.
- 11:57Speaker 2: It also immediately stops the function's.
- 11:59Speaker 1: Execution, so twice five would return ten.
- 12:02Speaker 2: Correct. And here's the connection back to undefined. If a
- 12:05Speaker 2: function doesn't have a return statement, or if it reaches
- 12:08Speaker 2: the end without hitting.
- 12:09Speaker 1: One, it implicitly returns undefined, just like console dot log.
- 12:13Speaker 2: Precisely, all functions return something if you don't specify it's undefined.
- 12:17Speaker 1: Okay, makes sense now. The book briefly touches on those
- 12:20Speaker 1: pop up dialogue boxes alert prompt confirmed.
- 12:24Speaker 2: Yeah, the old school ways to interact directly alert shows
- 12:27Speaker 2: a message, prompt asks for text input, confirm asks the
- 12:30Speaker 2: yes no question.
- 12:31Speaker 1: But they're kind of limited, right They block everything else totally.
- 12:35Speaker 2: They freeze the browser until the user interacts, and you
- 12:38Speaker 2: can't style them. Modern web apps use better techniques, like
- 12:42Speaker 2: Htmail's dialogue element, but the book uses these for simplicity
- 12:46Speaker 2: in early examples.
- 12:47Speaker 1: Gotcha, Now, how does this JavaScript code actually get onto
- 12:51Speaker 1: a web page?
- 12:52Speaker 2: Two main ways described Inline scripts You put your JavaScript
- 12:56Speaker 2: code directly inside script tags within your htmail.
- 12:59Speaker 3: File file, often in the head section often yeah.
- 13:03Speaker 2: Or the second way, external scripts, You put your code
- 13:06Speaker 2: in a separate JS.
- 13:06Speaker 1: File and link to it from the ADSTML using script
- 13:09Speaker 1: src pathworre your script adscript.
- 13:12Speaker 2: Exactly and a crucial point. If a script tag has
- 13:15Speaker 2: a src attribute pointing to an external file, any code
- 13:18Speaker 2: you write between the opening and closing script tags is ignored.
- 13:21Speaker 1: Okay, so it's either in line or external with src,
- 13:23Speaker 1: not both in the same tag.
- 13:25Speaker 2: Correct, And the scriptags can have other attributes type textuscript
- 13:28Speaker 2: used to be commer but it's mostly default.
- 13:30Speaker 1: Now what about async and defer ah?
- 13:32Speaker 2: Those control how the script loads and runs relative to
- 13:35Speaker 2: the HTML page being parsed. By default, when the browser
- 13:39Speaker 2: hits a script tag, it stops parsing, HTML, downloads the script,
- 13:44Speaker 2: runs it, and then continues parsing. This can slow down
- 13:47Speaker 2: page rendering.
- 13:48Speaker 1: Right makes the page feel slow to load.
- 13:50Speaker 2: ACYNC tells the browser download the script in the background
- 13:53Speaker 2: while parsing and run it as soon as it's downloaded,
- 13:56Speaker 2: which might interrupt parsing briefly. Order isn't guaranteed if you
- 13:59Speaker 2: have more ultiple ACNC scripts and defer. Defer also downloads
- 14:03Speaker 2: in the background, but it guarantees the script will only
- 14:05Speaker 2: run after the HTML parsing is completely finished and in
- 14:09Speaker 2: the order the Defer scripts appear in the HTML.
- 14:11Speaker 1: So defer is often preferred for scripts that need the
- 14:14Speaker 1: whole page structure to be ready.
- 14:16Speaker 2: Generally yes, especially for scripts that manipulate the page content.
- 14:21Speaker 2: The book also mentions cross.
- 14:23Speaker 1: Origin right for loading scripts from different domains can cause
- 14:26Speaker 1: issues if you're testing locally without a web server.
- 14:28Speaker 2: Yes, browsers have security rules about that. You might need
- 14:31Speaker 2: to remove frosts origin anonymous if you're just opening HTML
- 14:35Speaker 2: files directly from your computer, okay.
- 14:37Speaker 1: Error handling JavaScript used to be quiet about errors.
- 14:41Speaker 2: Historically, yes, sometimes things would just break silently, making debugging
- 14:45Speaker 2: and nightmare.
- 14:46Speaker 1: But strict mode helps immensely.
- 14:49Speaker 2: It makes JavaScript throw more explicit errors for common mistakes,
- 14:53Speaker 2: much better for catching bugs early.
- 14:55Speaker 1: And the book provides a basic template comments strict mode, and.
- 14:59Speaker 2: A simple window a function. It's like a safety net.
- 15:02Speaker 2: If any uncaught error happens, this function runs and the
- 15:05Speaker 2: template just uses alert to display the error message. Very basic,
- 15:10Speaker 2: but a.
- 15:10Speaker 1: Starting point okay, that covers the foundational theory. Chapter two
- 15:14Speaker 1: jumps into a project YEP.
- 15:15Speaker 2: A simple number guessing game, putting the basics into practice.
- 15:19Speaker 1: Starts with HTML just a button with egged start.
- 15:22Speaker 2: Game right, and the JavaScript uses document dot query selector
- 15:27Speaker 2: button Halftag's start game to grab that button.
- 15:29Speaker 1: Query selector is the modern way using CSS selector exactly.
- 15:33Speaker 2: Hashtag start game means find the element with the id
- 15:36Speaker 2: start game. Very powerful, and then.
- 15:38Speaker 1: It attaches an on click listener, so when you click the.
- 15:40Speaker 2: Button, it runs the play game function. That function contains
- 15:43Speaker 2: the game's logic.
- 15:44Speaker 1: How does it get the user's guess using prompt again.
- 15:47Speaker 2: Yes, prompt guess a number. Remember Prompt returns the typed
- 15:51Speaker 2: string an empty string or null if they cancel right.
- 15:54Speaker 1: And the game needs to check if the guess is right,
- 15:56Speaker 1: too high, or too low using if statements yep.
- 15:59Speaker 2: If guess answer too low. So on basic conditional logic and.
- 16:03Speaker 1: To let the user keep guessing, a loop a do.
- 16:06Speaker 2: Wile, loop dot do bake it's guests give feedback. Wild
- 16:10Speaker 2: guests answer the do.
- 16:11Speaker 1: Dot wile runs the code inside the do block at
- 16:14Speaker 1: least once before checking the condition.
- 16:16Speaker 2: Correct, which makes sense here. You always want the user
- 16:18Speaker 2: to make at least one guess.
- 16:20Speaker 1: And the book stress is testing trying different inputs.
- 16:23Speaker 2: Crucial test guessing too high, too low, the right number,
- 16:27Speaker 2: canceling the prompt, entering non numbers. See how it behaves.
- 16:30Speaker 1: To get the secret number, it uses math dot random.
- 16:34Speaker 2: Yeah, that gives a random decimal between zero and almost one.
- 16:36Speaker 1: So you need to scale it and make it a
- 16:38Speaker 1: whole number.
- 16:39Speaker 2: Right, You multiply by the maximum number you want example max,
- 16:42Speaker 2: usemath dot floor to top off the decimal part and
- 16:45Speaker 2: then add one to get range from one to max.
- 16:47Speaker 1: And the book puts this logic into a reusable.
- 16:50Speaker 2: Function exactly function random max retunemath dot floor math lot
- 16:54Speaker 2: ran plus one. Good practice to encapsulate logic like.
- 16:57Speaker 1: That, and it warns you could accidentally overwrite built in
- 17:01Speaker 1: functions if you name yours the same, like if you
- 17:04Speaker 1: called your function math dot random.
- 17:05Speaker 2: Well, not quite math dot random because math is an object.
- 17:09Speaker 2: But if there was a global random function, Yeah, naming
- 17:12Speaker 2: collations can happen, so choose descriptive names.
- 17:14Speaker 1: Okay, what about when the user clicks cancel in the
- 17:17Speaker 1: prompt it returns null. How does the game handle that gracefully?
- 17:21Speaker 2: Using the oer operator for default values like let guess
- 17:25Speaker 2: prompt guests zero.
- 17:26Speaker 1: Ah, So if prompt returns null, which is falsey, the
- 17:30Speaker 1: operator makes guests become zero instead.
- 17:33Speaker 2: Exactly prevents trying to compare null to a number later,
- 17:35Speaker 2: which would cause errors.
- 17:36Speaker 1: Nice trick. Then organizing code, putting random into a separate
- 17:40Speaker 1: library dot js file.
- 17:42Speaker 2: Yep, creating a function library for reusable code.
- 17:45Speaker 1: Good structure at comments, link it in the HTML with
- 17:47Speaker 1: scripts src scriptslibrary, dot.
- 17:49Speaker 2: Js and remember that cross origin attribute again if testing
- 17:53Speaker 2: locally right?
- 17:54Speaker 1: And the chapter ends by summing up comments, organization, loops, conditional.
- 18:00Speaker 2: Dialogues, all applied in a simple working game.
- 18:02Speaker 3: Okay.
- 18:03Speaker 1: Chapter three and moves into manipulating the web page itself,
- 18:07Speaker 1: the DOM.
- 18:07Speaker 2: The document object model super important. It's the browser's internal
- 18:11Speaker 2: representation of the HTML page like a structured tree, and JavaScript.
- 18:15Speaker 1: Can interact with this tree change things absolutely.
- 18:18Speaker 2: That's how you make pages dynamic. First step is finding
- 18:21Speaker 2: the elements you want to change.
- 18:22Speaker 1: We saw document dot. Query selector already finds the first
- 18:25Speaker 1: match for a CSS selector.
- 18:27Speaker 2: Yes, like query selector gets the first paragraph. Query selector
- 18:31Speaker 2: hashtag my ideald gets the element with that id, Query
- 18:35Speaker 2: selector dot my class gets the first element with that.
- 18:37Speaker 1: Class, and document dot.
- 18:38Speaker 2: Query selectoral that finds all matching elements and returns them
- 18:42Speaker 2: as a collection a notalist.
- 18:44Speaker 1: So query selectoral gets you all the paragraphs on the
- 18:46Speaker 1: page direct.
- 18:47Speaker 2: The book also mentions older methods like get element by eyed,
- 18:50Speaker 2: get elements by tag name, get elements by.
- 18:52Speaker 1: Class name, less used now, but might see them in
- 18:55Speaker 1: older code.
- 18:56Speaker 2: Pretty much. Query selector and query selectoral are generally or
- 19:00Speaker 2: flexible because they use CSS selectors. Notice the elements plural,
- 19:04Speaker 2: and the older ones often returned collections too.
- 19:06Speaker 1: Okay, So if qreer selectoral gives you a collection, how
- 19:09Speaker 1: do you work with all those elements?
- 19:11Speaker 2: Two main ways shown A standard for loop using the
- 19:15Speaker 2: length property of the noted list and accessing elements by
- 19:17Speaker 2: index number. My nodalisty for the.
- 19:19Speaker 1: Four each method.
- 19:20Speaker 2: Yeah, often cleaner my nod list dot four each that
- 19:24Speaker 2: uses an aero function as the callback. Okay.
- 19:26Speaker 1: Once you found an element, how do you change its content?
- 19:28Speaker 2: Two main properties dot text content and dot inner html.
- 19:32Speaker 1: What's the difference.
- 19:33Speaker 2: Text content gets or sets only the raw text inside
- 19:37Speaker 2: an element, ignoring any HTML tags, and inner HTML gets
- 19:41Speaker 2: or sets the entire HTML markup inside an element. If
- 19:44Speaker 2: you set it, the browser parses the string as HTML.
- 19:48Speaker 1: The book warns about security with intern HTML. Right if
- 19:51Speaker 1: the content comes from a.
- 19:52Speaker 2: User, big time. If a user can inject malicious HTML
- 19:56Speaker 2: or script tags via inner HTML, it's a security rip.
- 20:00Speaker 2: Cross site SCRIPTINGXSS. Use dots content whenever you just need
- 20:04Speaker 2: to change text.
- 20:04Speaker 3: Got it?
- 20:05Speaker 1: Intertext is also mentioned, but text content is preferred.
- 20:08Speaker 2: Yeah. Text content has more predictable behavior regarding spacing and
- 20:11Speaker 2: hidden elements.
- 20:11Speaker 1: Okay. What about changing attributes like the src of an
- 20:15Speaker 1: image or the hrafh of a link.
- 20:17Speaker 2: Often you can access them directly as properties of the
- 20:20Speaker 2: element object my image dot src e wal goals new
- 20:23Speaker 2: dot jpg, my link, dot HRAF equals newpage dot HTML.
- 20:27Speaker 1: Simple enough. What about CSS classes?
- 20:29Speaker 2: There's the older dot class name property setting. It replaces
- 20:33Speaker 2: all existing classes on the element with a new string.
- 20:35Speaker 1: You provide, which might not be what you want.
- 20:37Speaker 2: Usually not. The modern way is dot class list. It's
- 20:40Speaker 2: an object with methods like dot.
- 20:42Speaker 1: Ad new class, dot remove old class, dot toggle active
- 20:45Speaker 1: class exactly.
- 20:46Speaker 2: Toggle adds to the class if it's missing, removes it
- 20:48Speaker 2: if it's present, and dot contains some class checks if
- 20:51Speaker 2: the class exists. Much more precise control.
- 20:53Speaker 1: Why dot class name and class list. Why not do
- 20:56Speaker 1: dot class.
- 20:56Speaker 2: Because class is a reserved keyword in Javacript, its self
- 21:00Speaker 2: used for defining JavaScript classes, so they had to use
- 21:03Speaker 2: slightly different names for the HTML attribute.
- 21:06Speaker 1: Ah okay. Now what if you want to create entirely
- 21:09Speaker 1: new elements and put them on the page.
- 21:12Speaker 2: You use document dot create element. For instance, document dot
- 21:16Speaker 2: create element creates a new carerograph element in memory.
- 21:20Speaker 1: It's not on the page yet not yet.
- 21:22Speaker 2: Then need to insert it somewhere. The book shows inserted
- 21:25Speaker 2: Jason element position new element.
- 21:28Speaker 1: And the position can be like before begin, after begin,
- 21:30Speaker 1: before and after end relative to an existing element.
- 21:33Speaker 2: Correct gives you fine grain control over where the new
- 21:36Speaker 2: element goes.
- 21:36Speaker 1: There's also inserted Jason HTML.
- 21:38Speaker 2: Yeah, that's a shortcut. You give it the position and
- 21:41Speaker 2: an HTML string. It purses the string and inserts the
- 21:44Speaker 2: resulting elements directly. Convenient, but you don't get a direct
- 21:48Speaker 2: reference to the newly created elements like you do with
- 21:51Speaker 2: create element.
- 21:52Speaker 1: Key takeaway. Creating an element isn't enough. You have to
- 21:55Speaker 1: explicitly add it to the dom to make it.
- 21:57Speaker 2: Visible absolutely from the metal.
- 21:58Speaker 3: All right.
- 21:59Speaker 1: Four applies this dom stuff to build a slide show.
- 22:02Speaker 2: Yeah. A practical example basic HTML, a DIFP container, maybe
- 22:07Speaker 2: id slides and emptymg tag inside.
- 22:09Speaker 1: JavaScript links the library dot js from before and a
- 22:13Speaker 1: new slideshow dot js.
- 22:14Speaker 2: And slideshow dot gs has the defer attribute on its script.
- 22:17Speaker 1: Deech, meaning it runs after the HTML is parsed. Library
- 22:21Speaker 1: dot js doesn't have differ.
- 22:22Speaker 2: Right, implying the library needs to be ready earlier. Maybe
- 22:25Speaker 2: because slideshow dot j as depends on functions in it. Yeah,
- 22:28Speaker 2: and again that cross origin note For.
- 22:30Speaker 1: Local testing inside slideshow dot js, first step is grabbing
- 22:33Speaker 1: the container dive and the MG element using query.
- 22:36Speaker 2: Selector standard practice get references to the elements you'll manipulate.
- 22:39Speaker 1: Then timing, set time out and set interval.
- 22:42Speaker 2: Setting out my function five thousand runs my function once
- 22:45Speaker 2: after five seconds five thousand doll.
- 22:47Speaker 3: Second, and set interval.
- 22:48Speaker 1: My function two thousand runs my function repeatedly every two
- 22:50Speaker 1: seconds exactly.
- 22:51Speaker 2: Set interval is key for the automatic slide changing.
- 22:54Speaker 1: So the basic slideshow logic have an array of image
- 22:58Speaker 1: file names. You set interval to call a function every
- 23:00Speaker 1: few seconds. That function updates the src attribute of the
- 23:03Speaker 1: imag tag to the next file name in the array.
- 23:06Speaker 2: Right, you need a variable, say slide number, to keep
- 23:09Speaker 2: track of which image index you're currently on and increment
- 23:13Speaker 2: it each time. Looping back to zero.
- 23:14Speaker 3: At the end makes sense.
- 23:16Speaker 1: The book then suggests making the delay configurable.
- 23:19Speaker 2: Yeah, adding a delay parameter to the function that starts
- 23:21Speaker 2: the slide show like does slides three thousand. Give it
- 23:26Speaker 2: a default value too, So old code still works good
- 23:28Speaker 2: for backward compatibility.
- 23:30Speaker 1: How do you stop and start it? Like a pause button?
- 23:33Speaker 2: Set interval returns a unique ID number. You store that
- 23:36Speaker 2: ID in a variable. Okay, to stop it, you call
- 23:38Speaker 2: clear interval the stored ID.
- 23:40Speaker 1: Ah, So a toggle function could check if the ID
- 23:43Speaker 1: variable holds a number, meaning it's running. If yes, call
- 23:47Speaker 1: clear interval and set the variable to null exactly.
- 23:50Speaker 2: And if the variables null it means it's starved, so
- 23:52Speaker 2: you call set interval again to start it and store
- 23:54Speaker 2: the new ID.
- 23:55Speaker 1: And you'd hook this toggle function up to a button
- 23:57Speaker 1: using either dot on click or event listener click.
- 24:01Speaker 2: Tuggle sisely ad. Event listener is generally more flexible if
- 24:04Speaker 2: you need multiple listeners on the same element to make.
- 24:06Speaker 1: It feel smoother. The book suggests prefetching the next image yeah.
- 24:11Speaker 2: While when image is showing, you create a new image
- 24:14Speaker 2: object in JavaScript in the background like.
- 24:16Speaker 1: Let next image in a new image yep.
- 24:19Speaker 2: Then set it's src to the next slides url nextimage
- 24:22Speaker 2: dot src images next line number. This tells the browser
- 24:26Speaker 2: to start downloading it.
- 24:27Speaker 1: So when set interval fires and you actually change the
- 24:30Speaker 1: main images src, the image data is likely already cashed.
- 24:33Speaker 2: Hopefully leading to a faster, smoother transition. Improves perceived performance. Cool.
- 24:38Speaker 1: What about keyboard control?
- 24:40Speaker 2: You can listen for keypresses on the whole document document
- 24:43Speaker 2: dot on keypress sec event.
- 24:46Speaker 1: And the event object tells you which key is pressed yes.
- 24:49Speaker 2: The event dot keep property is the modern reliable way
- 24:52Speaker 2: it gives you strings like body for spacebar, aerolaft, biahm,
- 24:56Speaker 2: et cetera.
- 24:56Speaker 1: And you might use event dot prevent default, like to
- 24:59Speaker 1: stop a space bar from scrolling the page if you're
- 25:01Speaker 1: using it to pause play.
- 25:02Speaker 2: Exactly take control away from the browser's default action for that.
- 25:06Speaker 2: Key errol functions work nicely here too, Document dot onkeypress
- 25:10Speaker 2: event to make it more informative.
- 25:12Speaker 1: Add captions.
- 25:13Speaker 2: Yeah, change the images array instead of just file names,
- 25:15Speaker 2: making an array of objects. Each object has image dot
- 25:18Speaker 2: jpg caption a lovely photo.
- 25:21Speaker 1: So when you change the image, you update the mg
- 25:23Speaker 1: dot src, but also find a caption element like p
- 25:26Speaker 1: tag and set its dot text content.
- 25:28Speaker 2: Perfect, and also update the img dot alt and maybe
- 25:31Speaker 2: mg dot title attributes for accessibility and tooltips.
- 25:34Speaker 1: Okay, so Chapter four summary timing, set time out, set interval,
- 25:38Speaker 1: controlling intervals, clearinterval, event listeners on click at, event listener
- 25:42Speaker 1: on keypress, and enhancing with prefetching and captions.
- 25:45Speaker 2: Putting dominipulation to work with timing and user interaction.
- 25:48Speaker 1: Chapter seven then shifts focus a bit, looking specifically at
- 25:51Speaker 1: interacting with CSS and event listeners more deeply.
- 25:54Speaker 2: Right starts with a quick reminder CSS controls the visual appearance, colors, sponts,
- 25:59Speaker 2: layout visibility, the.
- 26:00Speaker 1: Animations, and it mentions that CSS handles a lot more
- 26:03Speaker 1: visual stuff now than it used to, less need for
- 26:06Speaker 1: JavaScript for symple effects.
- 26:07Speaker 2: Definitely things like hover effects, basic animations, often pure CSS now.
- 26:12Speaker 2: The chapter revisits how CSS gets included external link tags,
- 26:16Speaker 2: internal style tags, or inline style attributes.
- 26:19Speaker 1: Inline styles being generally less.
- 26:21Speaker 2: Manageable usually yeah, harder to maintain consistency. It also teases
- 26:26Speaker 2: creating style tags with JavaScript itself, which comes later.
- 26:30Speaker 1: Event listeners again the bridge between user actions and JavaScript.
- 26:34Speaker 1: Mentions event bubbling.
- 26:35Speaker 2: Yeah, the idea that an event like a click starts
- 26:38Speaker 2: with the specific element clicked the target and then bubbles
- 26:43Speaker 2: up the dom tree to its parent, its parent's parent,
- 26:45Speaker 2: and so on up to the document.
- 26:47Speaker 1: And on click and default AD event listener listen during
- 26:50Speaker 1: this bubbling phase.
- 26:51Speaker 2: Correct. There's also a capture phase where the event travels
- 26:55Speaker 2: down the tree first, but bubbling is more commonly used.
- 26:58Speaker 2: You can listen in capture phase with AD event listener
- 27:00Speaker 2: type listener.
- 27:01Speaker 1: True okay, and clarifies event dot current target versus event.
- 27:04Speaker 2: Dot target crucial difference. Event dot target is the element
- 27:07Speaker 2: where the event originated ug The specific thing clicked event
- 27:11Speaker 2: dot current target is the element the listener is actually
- 27:13Speaker 2: attached to, which might be a parent element if you're
- 27:15Speaker 2: using event delegation. Got it?
- 27:18Speaker 1: How can JavaScript change CSS directly.
- 27:20Speaker 2: Through the element's style property my element dot style dot color.
- 27:24Speaker 2: It kills red my element dot style dot margin top
- 27:27Speaker 2: equals ten px.
- 27:28Speaker 1: So you access CSS properties as JavaScript object properties. What
- 27:32Speaker 1: about properties with hyphens like background color.
- 27:35Speaker 2: You can't use dot notation because the hyphen isn't valid
- 27:37Speaker 2: and JavaScript identifies there.
- 27:39Speaker 1: Yeah.
- 27:39Speaker 2: Two ways, either use CamelCase my element dot style dot
- 27:43Speaker 2: background color equals blue, or bracket notation my element dot
- 27:47Speaker 2: style background color equals blue.
- 27:49Speaker 1: CamelCase seems common, but the more recommended way is often
- 27:52Speaker 1: using classes right via dot class list.
- 27:55Speaker 2: Generally, yes, it keeps your styling rules neatly in your
- 27:58Speaker 2: CSS files. Script just toggles classes on and.
- 28:01Speaker 1: Off like the example. Add a listener to an H
- 28:04Speaker 1: one on click event dot target dot class.
- 28:06Speaker 3: List dot toggle different color.
- 28:08Speaker 1: And you'd have a CSS rule like H one dot differentcolor,
- 28:11Speaker 1: dot blue or whatever. Much cleaner separation of concerns.
- 28:14Speaker 2: Can you remove listeners to remove event listener?
- 28:17Speaker 1: Yes, but you need to pass the exact same function
- 28:19Speaker 1: reference that you used with ad event listener.
- 28:21Speaker 2: So if you use anonymous function like element dot ad
- 28:24Speaker 2: event listener, you can't easily remove it. Not easily, you
- 28:27Speaker 2: usually need to define the listener as a separate named function.
- 28:30Speaker 2: First function handle click element dot ad event listener look
- 28:33Speaker 2: at a handleclick, Then you can do element dot remove
- 28:36Speaker 2: event listener clickcandleclick okay.
- 28:38Speaker 1: CSS transitions for smooth changes.
- 28:41Speaker 2: Yeah. Instead of style snapping instantly, they animate over time.
- 28:44Speaker 2: You define the transition in CSS which property how long
- 28:48Speaker 2: it takes duration, maybe a timing function like ease in out.
- 28:51Speaker 1: Then JavaScript just changes the property value.
- 28:54Speaker 2: Yep. If you have transition background color point five's ease
- 28:57Speaker 2: on an element and JavaScript changes element dot style dot
- 29:00Speaker 2: background color, the browser animates the color change over half
- 29:04Speaker 2: a second.
- 29:04Speaker 1: Can you set the transition itself?
- 29:05Speaker 3: With JavaScript?
- 29:06Speaker 2: You can set element dot style dot transition of course
- 29:09Speaker 2: opacity ones linear and then change element dot style dot opacity.
- 29:13Speaker 1: What if you change a property multiple times quickly before
- 29:17Speaker 1: a transition finishes, it.
- 29:18Speaker 2: Can get complicated the browser might skip intermediate steps. The
- 29:22Speaker 2: book introduces the transition.
- 29:24Speaker 1: Event with fires when when a.
- 29:25Speaker 2: CSS transition completes, you can listen for it element dot
- 29:29Speaker 2: ad event list function to run code after the animation
- 29:32Speaker 2: is done.
- 29:33Speaker 1: Useful for chaining animations or cleaning up. Finally, creating CSS
- 29:37Speaker 1: rules dynamically advanced stuff.
- 29:40Speaker 2: You can create a style element in JavaScript let style
- 29:43Speaker 2: l document dot, create element style, depend it to the
- 29:45Speaker 2: head yep document dot head dot a pin child style call.
- 29:49Speaker 2: Then you can access its sheet property style l dot
- 29:52Speaker 2: sheet and use methods like insert rule dot dynamic class
- 29:55Speaker 2: coolor dot green to add CSS rules as strings.
- 29:58Speaker 1: Wow. Okay, so programmatic control over styles.
- 30:01Speaker 2: Powerful, but use with care. Usually managing classes is simpler.
- 30:05Speaker 1: Chapter eight focuses on showing and hiding content. Uses an
- 30:08Speaker 1: example page showing hiding dot HTML.
- 30:11Speaker 2: Right with sections initially hidden by CSS.
- 30:13Speaker 1: How do CSS hide things the display.
- 30:16Speaker 2: Proper displayed none. It's the common way. It completely removes
- 30:19Speaker 2: the element from.
- 30:20Speaker 1: The layout, and to show it again, display dot block
- 30:23Speaker 1: for block elements or display dot inline exactly.
- 30:26Speaker 2: The example uses CSS rules like hide the div right
- 30:29Speaker 2: after an H two, but if the H two also
- 30:31Speaker 2: has the class open, then show the following div using
- 30:34Speaker 2: display dot block.
- 30:36Speaker 1: So JavaScript's job is just to toggle that open class
- 30:38Speaker 1: on the H two when it's.
- 30:39Speaker 2: Click precisely get all the h twos with query selectoral,
- 30:43Speaker 2: add a click listener to each inside the listener dot event,
- 30:46Speaker 2: dot target, dot class list dot toggle open, Simple and effective.
- 30:50Speaker 1: The chapter then detours briefly into variable hoisting.
- 30:53Speaker 3: Again just a.
- 30:54Speaker 2: Reminder, var declarations and function declarations are hoisted conceptually moved up,
- 30:59Speaker 2: let and com okay.
- 31:01Speaker 1: Then a more complex example highlighting nested lists inside an
- 31:05Speaker 1: open section.
- 31:05Speaker 2: Yeah, if you click an H two to open its div,
- 31:08Speaker 2: maybe you also want any o inside the parent lie
- 31:11Speaker 2: of that div. To get a highlight class.
- 31:12Speaker 1: Requires navigating the dom tree using parent note to go up,
- 31:16Speaker 1: then query selector to find the full yep.
- 31:18Speaker 2: Bit a dom traversal. Then toggle the highlight class on
- 31:21Speaker 2: the nested doll.
- 31:22Speaker 1: And it addresses fixing the logic so highlighting is removed
- 31:25Speaker 1: correctly when the section is closed.
- 31:26Speaker 2: Right, making sure the state is managed properly on toggle.
- 31:29Speaker 1: Then it suggests putting this toggle logic into a reusable
- 31:33Speaker 1: toggless function in library dot JS.
- 31:36Speaker 2: Good practice abstracting common patterns.
- 31:38Speaker 1: The chapter finishes by enhancing radio buttons, making them de selectable.
- 31:42Speaker 2: Yeah, normally you can only switch radio buttons, not turn
- 31:45Speaker 2: the current one off.
- 31:45Speaker 1: How do you make it de selectable.
- 31:47Speaker 2: Get all the radio buttons in the group agmform dot
- 31:51Speaker 2: elements group name, add a click listener to each one.
- 31:54Speaker 1: Inside the listener, check of.
- 31:55Speaker 2: The button you just clicked event dot target was already
- 31:59Speaker 2: checked before the click event fully processed. This is slightly tricky.
- 32:03Speaker 2: Might need to store previous date briefly or check within
- 32:05Speaker 2: the listener carefully if it was already checked. Set its
- 32:09Speaker 2: jecked property to false.
- 32:11Speaker 1: Ah okay allows toggling off, and this logic also gets
- 32:15Speaker 1: moved to a reusable de selectable radio function in the
- 32:18Speaker 1: library YEP.
- 32:19Speaker 2: The chapter summary mentions using CSS display, but also maybe
- 32:24Speaker 2: max height or opacity for transitions and JavaScript class toggling
- 32:28Speaker 2: for show heid effects cool.
- 32:30Speaker 1: Chapter nine another big project a light box gallery wreck.
- 32:34Speaker 2: A thumbnail see a larger version in an overlay.
- 32:36Speaker 1: Basic htmail gallery of thumbnail images, probably wrapped in anchor
- 32:40Speaker 1: a tags. The A graph of the anchor points to
- 32:42Speaker 1: the large image URL and.
- 32:44Speaker 2: Maybe some CSS to style the thumbnails like desaturate them.
- 32:48Speaker 1: Initially, the JavaScript starts with a dawn light box function.
- 32:51Speaker 1: First step find all those thumbnail anchor tags using query
- 32:55Speaker 1: selectorol YEP.
- 32:56Speaker 2: Then dynamically create the light box elements in Java.
- 33:00Speaker 1: Script a background div hashtag, light box background to dim
- 33:03Speaker 1: the page.
- 33:04Speaker 2: Right and a container. Maybe have a figure eyed light
- 33:07Speaker 2: box to hold a big MG, and maybe.
- 33:09Speaker 1: A caption u CSS to style these fixed position centered
- 33:13Speaker 1: semi transparent background exactly.
- 33:15Speaker 2: Then appen these new elements to the document body.
- 33:17Speaker 1: How does clicking a thumbnail trigger it?
- 33:19Speaker 2: Attach a click listener to each thumbnail anger inside the.
- 33:22Speaker 1: Listener first event dot, prevent default to stop the link
- 33:26Speaker 1: navigating crucial.
- 33:27Speaker 2: Then get the ape ref from the clicked anchor, which
- 33:30Speaker 2: is the URL of the large image.
- 33:32Speaker 1: Set the ASRC of the light box's MG element.
- 33:34Speaker 3: To that r right.
- 33:35Speaker 2: Maybe set the alt attribute to Then make the lightbox
- 33:38Speaker 2: visible by setting the display style of the background and
- 33:41Speaker 2: the container to block.
- 33:42Speaker 1: How do you close it?
- 33:43Speaker 2: Add a click listener to the background div when clicked,
- 33:46Speaker 2: set the display of the background and container back to none.
- 33:49Speaker 1: Keyboard navigation, escape key to clothes, arrow keys to go next,
- 33:54Speaker 1: previous listen for.
- 33:55Speaker 2: Key up on the document, use event dot key.
- 33:58Speaker 1: In a switch statement case escape dot hide, light box,
- 34:02Speaker 1: break case arrow left, show previous image, break.
- 34:06Speaker 2: Like that exactly. Home key for first image and key
- 34:10Speaker 2: for last How does.
- 34:11Speaker 1: Show previous image. Work needs refactoring.
- 34:13Speaker 2: Yeah, The book suggests storing a reference to the currently
- 34:16Speaker 2: shown thumbnails anchor tag. Let's call it current anchor okay,
- 34:19Speaker 2: and create a separate load image anchor to show function.
- 34:22Speaker 2: This function takes an anchor element, gets its ATRAF, sets
- 34:25Speaker 2: the light box image src, maybe updates.
- 34:27Speaker 1: A caption so the errorkey listeners just figure out the
- 34:30Speaker 1: next or previous anchor element in the dom.
- 34:32Speaker 2: Using things like current anchor dot parent, noode dot, previous
- 34:34Speaker 2: elements sibling, or next element sibling to find the sibling
- 34:37Speaker 2: a's parent, say maybe, then the next prey sibling then
- 34:40Speaker 2: it's child A.
- 34:41Speaker 1: And then call load image with that new anchor element.
- 34:44Speaker 2: Precisely need to handle wrapping around at the start ten
- 34:47Speaker 2: of the gallery. Two.
- 34:48Speaker 1: What about ensuring the image is loaded before showing mg dot.
- 34:51Speaker 2: Onload Yes inside load image before setting the light box
- 34:55Speaker 2: displayed a block, You could set the mmg dot src
- 34:58Speaker 2: that attach an onload listener to the image inside the
- 35:01Speaker 2: onload handler. You then make the light box visible prevents
- 35:05Speaker 2: showing an empty box.
- 35:06Speaker 1: Adding fade effects CSS transitions on opacity.
- 35:09Speaker 2: Yeah, add a CSS transition rule for opacity to the
- 35:12Speaker 2: light box container. Or image.
- 35:14Speaker 1: Then in JavaScript, when showing, after setting the iss and
- 35:16Speaker 1: maybe inside the onload, add a class like open that
- 35:20Speaker 1: sets opacity one.
- 35:21Speaker 2: Right, and when hiding, maybe add a closed class that
- 35:24Speaker 2: sets opacity zero. Wait for the transition using transition door,
- 35:27Speaker 2: set time out, and then set display none.
- 35:30Speaker 1: Nice. The chapter also revisits the slide show to add
- 35:33Speaker 1: a cross fade.
- 35:34Speaker 2: Briefly, yeah, more complex. Setup two mm and tags one
- 35:37Speaker 2: on top of the other. Hashtag cross fade absolutely positioned
- 35:40Speaker 2: inside a relative container.
- 35:42Speaker 1: CSS transition on the opacity of the top image.
- 35:44Speaker 3: Hashtag crossfait right, triggered.
- 35:46Speaker 2: By a class, say fading, which sets opacity the JavaScript logic,
- 35:50Speaker 2: copy the current images src to hashtag crossfade, make sure
- 35:54Speaker 2: hashtag cry sat opacity is one, then add the fading
- 35:57Speaker 2: class to start the fade out of the top image.
- 36:00Speaker 1: Okay, revealing the main image underneath.
- 36:02Speaker 2: Well almost. After adding fading, you wait for the transition
- 36:05Speaker 2: duration for example using set time out. Then you update
- 36:09Speaker 2: the main underlying mag tag with the next slide's src,
- 36:13Speaker 2: and finally reset the hashtag cross fade image, Remove fading,
- 36:16Speaker 2: maybe copy the new main image src to it ready
- 36:19Speaker 2: for the next fade. It's a bit fiddly, might need
- 36:21Speaker 2: to force a reflow sometimes too.
- 36:23Speaker 1: Sounds complex but creates a nice effect.
- 36:25Speaker 3: Okay.
- 36:25Speaker 1: Last chapter covered Chapter ten introduction to.
- 36:28Speaker 2: AJX, a syncretist JavaScript and XML. They're now mostly Jason.
- 36:32Speaker 1: The key idea fetching data from a server without a
- 36:35Speaker 1: full page reload.
- 36:36Speaker 2: Exactly makes web apps feel much more responsive like desktop applications,
- 36:41Speaker 2: think dynamic updates, loading content on demand. The previous projects
- 36:44Speaker 2: already did a simple version by fetching images.
- 36:47Speaker 1: It mentions the shift from the old XML HTTP request object.
- 36:51Speaker 2: To the modern fetch API, using the fetch function much
- 36:53Speaker 2: cleaner syntax.
- 36:54Speaker 1: The big challenge is handling the asynchronous part. JavaScript doesn't wait.
- 36:58Speaker 2: Right fetch and JavaScript immediately moves on to the next line.
- 37:03Speaker 2: The server response arrives later.
- 37:05Speaker 1: How do you handle that delayed response?
- 37:07Speaker 2: Promises Yes, fetch returns a promise object. A promise represents
- 37:12Speaker 2: the eventual result of an asynchronous.
- 37:14Speaker 1: Operation, and you use dot then to say what should
- 37:18Speaker 1: happen when the promise resolves, I e. In the server response.
- 37:21Speaker 2: Exactly fetch my data dot Jason that handle response. Handle response.
- 37:24Speaker 2: The function inside only runs after the response arrives, and.
- 37:27Speaker 1: The response object isn't the data itself, yet no, it.
- 37:30Speaker 2: Represents the HTTP response. You need to call methods on
- 37:33Speaker 2: it to extract the body data like response dot text
- 37:36Speaker 2: or response.
- 37:36Speaker 1: Dot json, and those methods also return promises because reading
- 37:40Speaker 1: the body can also take time.
- 37:41Speaker 2: Correct so you often chain dot then calls dot fetch
- 37:44Speaker 2: data dot json, dot evil, then data out out data
- 37:48Speaker 2: holds the part Jason objectory.
- 37:50Speaker 1: Okay, that structure makes sense, and Jason is the common data.
- 37:53Speaker 2: Format very common JavaScript object notation, human readable text format
- 37:58Speaker 2: that looks just like JavaScript objects in arrays, easy for
- 38:01Speaker 2: JavaScript to parse, easy for servers to generate.
- 38:05Speaker 1: The chapter shows fetching a slides dot json file an
- 38:08Speaker 1: array of image objects.
- 38:09Speaker 2: YEP src, MMG one dot jpg caption. First netch uses
- 38:14Speaker 2: fetch and json to get this array into a JavaScript variable,
- 38:18Speaker 2: and then.
- 38:18Speaker 1: Uses this data to dynamically build the light box gallery exactly.
- 38:22Speaker 2: Instead of having the HTML hard coded, it loops through
- 38:26Speaker 2: the fetched deray. For each object, it creates the A
- 38:28Speaker 2: and MG elements using maybe insert adjason HTML.
- 38:32Speaker 1: Puts them into a containertiv hashtag catalog.
- 38:34Speaker 2: And then calls the do light box hashtag catalog function
- 38:37Speaker 2: we discussed earlier to make the newly created thumbnails.
- 38:39Speaker 1: Interactive, so the gallery content comes from the server on demand. Cool.
- 38:43Speaker 1: Final example populating a form with fetch data.
- 38:45Speaker 2: Yeah, an HTML form with outputs and a select drop down.
- 38:49Speaker 1: Step one fetch a list of countries, maybe IDs, and
- 38:52Speaker 1: names from the server as JSON.
- 38:54Speaker 2: Right loops through the fetched de array and dynamically create
- 38:56Speaker 2: option elements, adding them to the select dropdown.
- 38:59Speaker 1: Step two add an event listener to the select element.
- 39:02Speaker 1: When the user chooses a country.
- 39:04Speaker 2: The listener triggers another fetch request, this time asking the
- 39:08Speaker 2: server for detailed data about the selected country using its ID.
- 39:11Speaker 1: Step three. When that detailed data arrives, another promise resolution.
- 39:16Speaker 2: The JavaScript updates the output fields in the form output, span, etc.
- 39:21Speaker 2: With its specific details continent, currency, whatever.
- 39:24Speaker 1: So the form populates dynamically based on user selection, fetching
- 39:27Speaker 1: only the data needed.
- 39:29Speaker 2: Exactly, a very common pattern in modern web applications.
- 39:31Speaker 1: Wow okay, So wrapping this up, we've really gone from
- 39:35Speaker 1: the absolute basics variables syntax.
- 39:39Speaker 2: Through manipulating the page with the DOM, handling user.
- 39:41Speaker 1: Events, interacting with CSS for styling and transitions.
- 39:44Speaker 2: All the way to asynchronously fetching data from a server
- 39:47Speaker 2: using ajax and fetch. It's quite a journey.
- 39:50Speaker 1: It really lays the groundwork for understanding how modern interactive
- 39:54Speaker 1: websites are built. JavaScript is just everywhere under the hood.
- 39:58Speaker 2: It's incredibly powerful and versatile. This teep dive gives you,
- 40:01Speaker 2: hopefully a really solid conceptual map.
- 40:03Speaker 1: Absolutely and if you want to go deeper, definitely experiment
- 40:06Speaker 1: play with the code. The book mentions resources like MDM,
- 40:09Speaker 1: webdocs and stack.
- 40:10Speaker 2: Overflow indispensable resources for any web developer.
- 40:14Speaker 1: Cannot recommend them enough for sure, So as we finish,
- 40:17Speaker 1: here's something to think about. This ability for a webpage
- 40:20Speaker 1: to change itself dynamically, to talk to servers without reloading
- 40:24Speaker 1: it fundamentally changed the Internet, didn't it completely.
- 40:27Speaker 2: It's the foundation of web applications as we know them.
- 40:30Speaker 1: So consider how this power continues to evolve. What new
- 40:34Speaker 1: kinds of interactions, what new online experiences might this dynamic
- 40:38Speaker 1: capability make possible in the future.