Exercise 12 Solution

Modern JavaScript Manipulating HTML Pages
12 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€96.21
List Price:  €134.70
You save:  €38.49
£79.93
List Price:  £111.90
You save:  £31.97
CA$143.79
List Price:  CA$201.32
You save:  CA$57.52
A$160.48
List Price:  A$224.69
You save:  A$64.20
S$135.83
List Price:  S$190.17
You save:  S$54.33
HK$777.07
List Price:  HK$1,087.93
You save:  HK$310.86
CHF 89.45
List Price:  CHF 125.24
You save:  CHF 35.78
NOK kr1,141.95
List Price:  NOK kr1,598.78
You save:  NOK kr456.82
DKK kr717.64
List Price:  DKK kr1,004.72
You save:  DKK kr287.08
NZ$177.52
List Price:  NZ$248.53
You save:  NZ$71.01
د.إ367.25
List Price:  د.إ514.17
You save:  د.إ146.91
৳11,994.74
List Price:  ৳16,793.12
You save:  ৳4,798.37
₹8,496.63
List Price:  ₹11,895.62
You save:  ₹3,398.99
RM450.75
List Price:  RM631.07
You save:  RM180.32
₦155,628.43
List Price:  ₦217,886.03
You save:  ₦62,257.60
₨27,934.96
List Price:  ₨39,110.06
You save:  ₨11,175.10
฿3,444.45
List Price:  ฿4,822.37
You save:  ฿1,377.92
₺3,518.54
List Price:  ₺4,926.10
You save:  ₺1,407.55
B$617.53
List Price:  B$864.57
You save:  B$247.04
R1,837.80
List Price:  R2,572.99
You save:  R735.19
Лв188.31
List Price:  Лв263.64
You save:  Лв75.33
₩144,872.25
List Price:  ₩202,826.95
You save:  ₩57,954.69
₪364.89
List Price:  ₪510.86
You save:  ₪145.97
₱5,883.01
List Price:  ₱8,236.45
You save:  ₱2,353.44
¥15,672.80
List Price:  ¥21,942.55
You save:  ¥6,269.75
MX$2,028.91
List Price:  MX$2,840.56
You save:  MX$811.64
QR366.34
List Price:  QR512.89
You save:  QR146.55
P1,382.59
List Price:  P1,935.69
You save:  P553.09
KSh12,923.70
List Price:  KSh18,093.70
You save:  KSh5,170
E£5,091.27
List Price:  E£7,127.98
You save:  E£2,036.71
ብር12,764.56
List Price:  ብር17,870.89
You save:  ብር5,106.33
Kz91,790.82
List Price:  Kz128,510.82
You save:  Kz36,720
CLP$99,155.08
List Price:  CLP$138,821.08
You save:  CLP$39,666
CN¥729.81
List Price:  CN¥1,021.77
You save:  CN¥291.95
RD$6,104.52
List Price:  RD$8,546.58
You save:  RD$2,442.05
DA13,454.60
List Price:  DA18,836.98
You save:  DA5,382.37
FJ$232.11
List Price:  FJ$324.96
You save:  FJ$92.85
Q773.21
List Price:  Q1,082.52
You save:  Q309.31
GY$21,000.46
List Price:  GY$29,401.48
You save:  GY$8,401.02
ISK kr13,903.60
List Price:  ISK kr19,465.60
You save:  ISK kr5,562
DH1,007.40
List Price:  DH1,410.40
You save:  DH403
L1,841.78
List Price:  L2,578.57
You save:  L736.78
ден5,922.50
List Price:  ден8,291.74
You save:  ден2,369.23
MOP$803.42
List Price:  MOP$1,124.82
You save:  MOP$321.40
N$1,834.70
List Price:  N$2,568.66
You save:  N$733.95
C$3,694.32
List Price:  C$5,172.20
You save:  C$1,477.87
रु13,665.58
List Price:  रु19,132.35
You save:  रु5,466.77
S/374.71
List Price:  S/524.60
You save:  S/149.89
K406.86
List Price:  K569.63
You save:  K162.76
SAR375.72
List Price:  SAR526.02
You save:  SAR150.30
ZK2,777.96
List Price:  ZK3,889.25
You save:  ZK1,111.29
L478.81
List Price:  L670.35
You save:  L191.54
Kč2,418.25
List Price:  Kč3,385.65
You save:  Kč967.40
Ft39,800.47
List Price:  Ft55,722.25
You save:  Ft15,921.78
SEK kr1,103.66
List Price:  SEK kr1,545.17
You save:  SEK kr441.50
ARS$102,190.76
List Price:  ARS$143,071.15
You save:  ARS$40,880.39
Bs693.61
List Price:  Bs971.08
You save:  Bs277.47
COP$437,799.12
List Price:  COP$612,936.28
You save:  COP$175,137.16
₡50,480.33
List Price:  ₡70,674.48
You save:  ₡20,194.15
L2,548.03
List Price:  L3,567.35
You save:  L1,019.31
₲784,126.06
List Price:  ₲1,097,807.85
You save:  ₲313,681.79
$U4,460.41
List Price:  $U6,244.75
You save:  $U1,784.34
zł409.96
List Price:  zł573.96
You save:  zł164
Already have an account? Log In

Transcript

It's time to take a look at the solution for exercise 12. Hopefully you were successful, but if not, we're going to walk through it together. So let me jump to sublime. This is the app.js file in the state, it was set up for you at the start of this exercise. So the first thing I want to do is I want to take care of the event that's going to call this initialize function. So I'm going to put that below the function that's been created.

Do window dot add eventlistener. And then the event I'm going to look for is load and I will call it simple as that. Now just for your information, there is also an event DOM content content loaded, that sometimes gets used for the same purpose. The main difference between those two is load wait until Not only is the HTML page loaded, but all of the assets are loaded as well. So for example, like images, and CSS files, and whatnot, DOM content loaded just waits till the HTML pages is ready to parse. So you're aware because you may see that event used.

Now we've got this initialize function being called. Let's go ahead and set up the rest. So first off, before I set up the handlers, I'm going to create two functions. One is going to handle going to the next page and then one is going to handle going to the previous page. So I'm just going to call these go next. And then I would like to include an If statement.

So, generally, the way we're going to go to the next page is we'll use the page counter variable, we will increment it. And then we'll use it to pull the page out of the pages array, and then set the location to the new page using the Assign method that I introduced to you when I introduced this exercise, so that's the process we're going to use. And so an easy way to do that would be simply increment page counter, and then set the new page using the location object dot assign method. However, what if the page counter goes too high? Do you want to catch that so that it prevents from causing an error? I do and so I'm going to put an if statement here to try to avoid that happening.

So I want to check this. Make sure That page counter is less than pages dot length minus one. If it's less than pages dot length minus one, then that means there is still a page I can go to, because this is paid, this would be equal to pages dot length minus one. So if our page counter is less than that, I can increment it and go to the next page. So now I would increment page counter. Whoops, forgot my semicolon, semicolon.

Now, I want to load the new page, I'm going to use the when variable dot location, that's the location object and then the assigned method. And I'm going to pass to the Assign method, the page from the pages array that is now equal to page counter. Oops. And close that with a semicolon. All right, that will take us to the next page. Let's go ahead and put a semicolon at the end of that expression.

Now let's create go previous. And I'll call this function go previous. Now the reason I'm creating a function, instead of just putting the code as a part of the listener, is because I want two different listeners to execute the same code. I want the listener that's for the key. Press analyst, enter this for the mouse click. I want both.

And so in order not to duplicate that same code, I'm creating it here. And then I will use it as a part of the listeners that I'll create next. Now once again, we want to check page counter. So we don't go to a number below zero, because that would give us an error because nothing would exist there. So I'm going to enter an if statement if page counter. And we'll simply check to see if it's greater than zero.

If it's greater than zero, we can decrement it and still get a page from the pages array. So let's decrement our page counter variable and then let's use the Assign method to load that page. The page counter variable points to in the pages array. All right, that looks pretty good. We've got our go next and our ago previous functions set up. Now we can go ahead and add our event listeners.

So let's first add the event listener for the key press. So when dot add event listener going to add it to the window object, I could add it to the document as well. I'm going to use key down as the event. There are three events associated with keys, key down key, press and key up, and I'll be using key down. Now we need to enter the function that's going to be executed. Well, you may be thinking, just enter going after go previous, but I can't quite do that yet.

I need to first check what key is pressed and then I know whether to call go next or go previous. So I will enter an anonymous function. Inside of parentheses, I'm going to put an E variable to capture the event object. press return, let's define it. So first and if statement. Let's check to see what the key code is.

Now I happen to already look this up. So I know that key code for the left arrow key is 37. I'm going to put a comment there for future reference. So that somebody's looking at this code later on when they'll that's the left arrow, arrow key. Then if the left arrow key is pressed, I want to call go previous. And you know, I'm going to go ahead and pass the event object.

Like I said, I'm not going to use it. But just in case in the future, I need to plus i will be calling these functions using the mouse click and In that case, instead of creating an anonymous function, I'm going to call them directly. And so I'm going to set these up to capture the event object anyway. So in that case, I'd want to pass this event object to the appropriate function as well. So that takes care of the left arrow key. Go previous.

Now, continue with our if statement. elsif. key code is equal to 39. And 39 is the right arrow key. So once again, I'm going to put a comment for that too. And then of course, we will call go next.

Put a semicolon at the end of that. event listener definition. All right, we have the one for the keystroke. Now, we need to add In event listener to that left and right arrow, well, so far I don't have anything in here to refer to the left and right arrow. So let's take a look at one of these pages. Notice that the ID for the left arrow simply left their ID is right arrow for that right arrow image.

So let's create a couple more variables just to pull those in. I'm just gonna call it left arrow, call the same thing is equal to document dot query. I'm going to use query selector I could use get element by ID. I just tend to favor query selector. So that will get us to the left arrow. Now let's grab the right arrow.

We enter the hash symbol, remember to indicate that it is an ID that we're after. We only have to do that with query selector, if we would have used get element by ID, we could have just entered left arrow or right arrow without the hash. Alright, now we have those two DOM nodes, and we can add an event listener to them. So down here below our key event listener. I'll do left arrow first, left arrow dot add event listener. And this is the click event.

Now the function we want to execute if it's the left arrow is go previous. So in this case, I don't need to do loops an anonymous function. I simply enter that the function that I've already defined up here that takes care of left arrow Let's do right arrow, add event listener, the event is click and go next. All right, those are our two handlers for the left arrow and the right arrow. We've got our handler for the, the keys. Everything's looking pretty good.

Let's go ahead and save this and try it out. So I'm going to start with page one. Let me just grab the file path to that. So I can open that to begin with. Now let's first see if we can go to the next page with the right URL. So I clicked there.

Sure enough agos, I can go back. Yes. Let's try the keys. Now. That's working great. Going back, working great.

How about if I press the key to go to the last page can I then go back using the arrow Sure enough, so those are working together as well. Which is nice and something that we needed to test. So it looks like things are working. Hopefully you were able to accomplish the same task, you may have done things a little bit differently. And that's one of the things that I really love about JavaScript is there are multiple ways to do things. If you did do it in a different way.

Hopefully you learn something from the way I did it as well. All right, let's move on to the next topic.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.