Class Checkpoint

Making Parkwood Vale · Part 6 of 13

Part 6

Build the home page image slideshow in JavaScript, and hide .php from addresses with .htaccess.

We write our own JavaScript slideshow for the home page gallery, debug it, and tidy the site's addresses. In this part: 0:33 How the slideshow works by showing and hiding images 1:30 The slideshow container and the markup for each slide 6:04 CSS to centre the slideshow 7:19 slideshow.js: hiding every slide 9:52 Linking the script, and why removing defer fixed it 11:26 Cycling through the slides every 4 seconds with setTimeout 13:21 Debugging the last slide 16:02 Tidying the code and moving the gallery into place 19:11 An .htaccess file so pages load without .php in the address Software: Adobe Dreamweaver, UwAmp, JavaScript, HTML, CSS, Apache.
Transcript
  1. In the last video then we looked at setting up our template pages and adding in the
  2. content for our index page. We also added in a little bit of content for our
  3. facilities page but nothing massive.
  4. What I said in the last video was we were going to come back and revisit the
  5. interactive image gallery and look at how we can add some JavaScript to control
  6. that. That's what we're going to be doing today.
  7. So let's get into it.
  8. Okay, so here I am back on my student desktop.
  9. I'm going to go in and launch UwAmp.
  10. So I've got access to my site and reopen Dreamweaver.
  11. Now, the logic behind this, the way that this works, is that the pictures are always
  12. there. If I open up Parkwood Vale and we look at the source
  13. code for it, so view page source you will see that the images
  14. are loaded in and they all already exist it's not that they are being
  15. added in retrospectively or anything like that the trickery is that it's displaying
  16. and hiding those images so here's my index page let's reconnect to my test
  17. site a second and on my index page then what I'm going to do underneath the intro is
  18. put in this section
  19. and I'm just going to put in the end tab for my gallery as well.
  20. So with that in place, I'm going to start putting in the divide sections and all the
  21. different content that will be used to create my slideshow.
  22. So I'm going to put in my first container.
  23. And in fact, actually, I'm going to put straight into the div,
  24. into this section here.
  25. Div class, and it is going to be a class of
  26. slideshow.
  27. And I've got that there.
  28. Let's just drop that in and tidy this up ever so slightly.
  29. Now, when working within slideshow, I'm going to have a div
  30. class which is called my slide
  31. and that is going to be where my pictures are stored.
  32. I have three pictures in my image folder the three different hero images set up
  33. we've got the animal farm hero image we've got the Harry Potter hero image and in
  34. there somewhere is the Lord of the Rings hero image they're the three that I'm going
  35. to be working with and I got that set up in place here.
  36. Now inside that, I'm gonna add in a picture tag
  37. and there's the end of my picture tag and then I'm gonna add in my source set
  38. and put in the link to the images.
  39. So I want it to be images.
  40. No, there's no S. I've got to keep remembering not to put the S in place.
  41. I want the webp image. And the first one I want is the Harry Potter
  42. hero.
  43. Dot webp.
  44. It's a type of image forward slash
  45. webp.
  46. Now, that in place would create one picture in that section, but I want to put an
  47. image tag in as my fallback. The source, again, is going to be equal
  48. to, and I can browse this time around because of source, and I can go into image,
  49. png, and it's the Harry Potter hero image.
  50. with that in place then I want to say that the width or the class to start with
  51. actually is equal to MX auto just like I did on my other images and display is equal
  52. to block
  53. and that the width is set to 100% of its container and the alt
  54. text for the accessibility features is going to say Harry
  55. Potter hero image.
  56. Now, with all of that in place there, if I save this and I put this up,
  57. and that is just uploading all the files, make sure they're all in place, and I go
  58. to my local host,
  59. I will see the Harry Potter image.
  60. Ignore the fact about the HTTPS element there.
  61. It is on my local host,
  62. I want to put the other hero images in place as well.
  63. So I'm going to copy this text down, place it here,
  64. and the same here and place it there and just change the different
  65. elements.
  66. Oh, wrong place. That's where I want that one.
  67. And I want a lot of the rings.
  68. and obviously I change what this says here.
  69. And then
  70. if I just want to put the one individual page up this time and I refresh it,
  71. you'll see that all of those images exist.
  72. the same functionality as we see when we go to Parkwood Vale we only get the one
  73. hero image and after a certain amount of time it changes the next one that's
  74. controlled through css and javascript so we're going to start off back in our
  75. style css document so style css here and we're going to be putting in some
  76. commands, some elements to help with our slideshow, our slider.
  77. Now,
  78. first thing I'm going to do is instead of using a tag wide rule, I'm going to
  79. attach it to a class. I do that by using a full stop.
  80. The class I'm looking to attach it to, I didn't mean to pop that out.
  81. Let's just check that back in there, is the slideshow class there.
  82. So I'm going to say slide-show and then I
  83. can put in my curly braces. What that's going to do is anything with a slideshow
  84. class now will have these properties applied to it.
  85. What I'm going to do is say the position for it is relative
  86. and the margin to start with is
  87. auto just to make sure that it stays in the center of my screen.
  88. Now that in place, save that. Let's put that up as well.
  89. Refresh this. And there we go.
  90. We're quite happy. Nothing has really changed on that regard.
  91. We're then going to move on and we're going to have a look now at setting up our own
  92. slideshow JavaScript.
  93. So in my JavaScript folder, I'm going to add a new file.
  94. I'm going to call it slideshow
  95. and put that as a JS file.
  96. If I open that up, it's going to be an empty JS file, and I'm going to start putting
  97. my code in place for this.
  98. So I'm going to start off by saying let to set my slide index
  99. equals zero.
  100. Now, every time my page loads, every time the script is called, it's going to set
  101. slide index to zero. and then I'm going to use my show slides
  102. function, which I haven't yet written.
  103. I'm going to put in a function, unsurprisingly called show slides,
  104. and then I'm going to add in the curly braces for it.
  105. So that is referencing here and calling that to get started.
  106. I'm going to say let i equals zero as my counter.
  107. and I want to say let slides equals document which is our
  108. current web page dot get element by class
  109. name this one here and the class name I want is my slides
  110. so that is saying get everything in my
  111. index page here that has a class called my slides so if I added more hero images
  112. I wouldn't update how many I've got, it automatically does it for me.
  113. And then I very simply say for i equals
  114. zero to ensure that it doesn't go any higher,
  115. while i is less than slides.length
  116. to make sure that it gets the full length of my slides,
  117. i++ to loop through. and I want to say that, and I check this in
  118. KU braces just to be sure,
  119. we'll test in a second though, I don't think we need the KU braces but I'll double
  120. check,
  121. slides, so these here, at position I, so that would be 0, 1 and 2,
  122. I want to say it's style tag, I want to display
  123. and I want the display to be none.
  124. Now if I save that put that and go into my footer
  125. and I attach that so I've got my script here I'm gonna copy it paste
  126. it
  127. confuse myself a second there tap that in and let's say instead of bootstrap bundle
  128. that says slideshow.js and put that as
  129. well we should not have any pictures.
  130. What part of that didn't work then?
  131. Let's see.
  132. Slideshow.js is there. Slideshow.js is here.
  133. That's all in place. It could be back to that defer command.
  134. So don't want that one. I want this remove defer and save that
  135. and re-put that just to be sure now
  136. let's see what happens if I refresh this okay it's not happy with that for some
  137. reason let's have a not look at the source I don't want that I want to go into
  138. inspect and see if I can get any errors at all nope
  139. not getting any errors. I can see all of this element here.
  140. Slideshow. Div class my slide.
  141. And not being hidden.
  142. So my slide.
  143. Slideshow.js. My slides.
  144. Pluralization will be the death of me, I swear.
  145. Let's put that back up then and refresh this.
  146. And there we go. Exactly like I expected to happen.
  147. they've disappeared.
  148. Now that's all well and good but we don't want it to disappear.
  149. What we actually want to do is increase my slide index and go on and show the
  150. next slide. So I'm going to say simply slide index which starts off at zero
  151. add one to it plus itself add plus plus
  152. but I'm going to say that if slide index is greater than the
  153. slides.length
  154. then I want my slide index and let's check this down on the next line here
  155. slide index to equal zero because what
  156. that's going to do is loop it back around so that it it goes back to the starting
  157. point. So if it goes higher than my current number of slides, it goes back to the
  158. start.
  159. Underneath there, I'm going to say slides.
  160. That's these here. And actually, it's this command I'm going to be redoing, but
  161. let's put it in full. At position slide index.
  162. So starting off on 0, 1, 2, and so on.
  163. Style.
  164. Again, there we go.
  165. And instead of saying none this time, I'm going to say block.
  166. So what it will do is it will show that one.
  167. I'm then going to set a timeout,
  168. which will allow me to specify what function I want to call, which is going to be my
  169. show slides itself recursively,
  170. and a time that I want it to wait in milliseconds.
  171. So if I save that now and re-put that, what should happen is we
  172. should see one of our images. And then after 4,000 milliseconds, it changes to my
  173. next image. For another 4,000 milliseconds, it changes to my third image.
  174. Oh, something's not right. Let's go back in.
  175. So instead of saying zero, try that at one and save that.
  176. And see what happens when I put that.
  177. and refresh this.
  178. Working just as expected, just as before.
  179. That third one doesn't want to load.
  180. So let's take our slide index here then, because don't forget slide index we added
  181. to this section here.
  182. It's like you take that out and do the add just before we recall itself.
  183. And that should now be able to go back to zero.
  184. Let's put that up and see what happens.
  185. Harry Potter this time, starting at our zero position image, not our first image.
  186. Our second image is showing properly.
  187. Our third image is showing properly.
  188. And here's the clincher. Does it go back to Harry Potter?
  189. No, it's gone again to the wrong one.
  190. So that's going slide index plus plus.
  191. Let's drop this function underneath here so we can test if that's in the right
  192. place. Push that up and refresh our page.
  193. perfect as expected
  194. onto jrl tolkien
  195. and still not getting a harry potter the last one there let's have a look we're
  196. getting an error in our element here undefined tag style okay so
  197. let's write to our console what position we're at so
  198. slide index let's have a look at what's going on slide index comes in at zero it
  199. shows it that's fine then it adds one to slide index if it's greater than that so
  200. slide index now should be
  201. one it should go through two three
  202. i think that's my issue is the fact that we have three in our
  203. image but there's that element there i reckon if i left it if that error
  204. didn't occur there
  205. it would go through properly. So we need to do slide length here
  206. minus one so we get that correct.
  207. And let's put that up, refresh our page, and see if that's
  208. fixed that issue there.
  209. moment of truth harry potter hooray it's worked so let's
  210. go through very quickly and add some comments in here comments in javascript are
  211. just forward slashes so calls show slide function
  212. in here then i'm going to leave let zero let's face it we can all understand what
  213. that's doing
  214. finds all the divs with
  215. the class my slide.
  216. And then this one is looping through, hiding
  217. all the slides.
  218. display slide at position slide index.
  219. We should know what that's doing.
  220. I didn't want to slide index. We say, if slide index
  221. is greater than number
  222. of slides,
  223. loop back.
  224. and then finally call itself
  225. after
  226. 400 mil
  227. a seconds now we've got that in place save that
  228. the comments don't really add much to it but it does allow us to see what's going on
  229. and javascript Let's put that in place.
  230. Now, I did want to try something.
  231. Let's see if it still works without the additional
  232. brackets in place. Because effectively, that should just execute the next line.
  233. And so should that one. Tied in our code up, reducing the amount of code that's
  234. actually being processed and giving us, hopefully, the same effect.
  235. So that has loopback.
  236. And it's still working exactly the same way.
  237. that extra bracket. Now, let's put that in place.
  238. That's all set up. We have our image gallery there.
  239. If I go back onto Parkwood Vale, it's
  240. not identical as
  241. there are a few styling differences with the fonts and different bits and pieces,
  242. but we do have those elements in place.
  243. And actually, on the original, I put the gallery first.
  244. Now because of the way that I've set this up with my gallery in a gallery tag there,
  245. I can just highlight my gallery tag, cut it from there,
  246. paste it there,
  247. save it, let's go index page and let's put that up to my website.
  248. If I refresh this, it swaps it around.
  249. So now we do have a very similar look to it and they're in the same positions.
  250. The only difference being that on here I've edited the distance of our container.
  251. Here it's a bit more centralized.
  252. One thing I have done as well is on my facilities page if I click onto it it
  253. no longer shows that I have access to or no longer needs requires I should say
  254. that dot php element to it and the way I did that is using a
  255. special document
  256. files here if I show hidden items it's not in this one it's in the UwAmp version
  257. called a .htaccess file. Now this is a file that's
  258. recognized by Apache web server and allows us to put in some commands so
  259. that Apache will do different things for us.
  260. Let me just close these down here and I'll take you through what it is I've done.
  261. So in this file here if I drop it into place here should open
  262. .htaccess file. It starts off saying turn on the rewrite engine.
  263. It then says to rewrite condition, my current request ends
  264. with anything inside here.php.
  265. And then add anything after it as well.
  266. If that's true, then use the rewrite rule of percentage one.
  267. Rewrite condition here, request the file name.
  268. name dot php and load that file name there so what that will do by having this
  269. in place it removes the trailing extension for the dot php section
  270. like i said that's on my web server and that is a dot htaccess
  271. file it's a hidden file if i was to delete that
  272. and i was going to go back to parkwood vale my local host would still work in
  273. exactly the same way. When I went to facilities now, it's going to tell me it's not
  274. there because it can't see that .php file.
  275. So here's my htaccess rules. I'm going to save this file as
  276. .htaccess and it's in my documents here.
  277. I'm going to click save.
  278. I've got the .htaccess file can't be seen.
  279. It is a hidden document that's going to Parkwood Vale here.
  280. And if I go there, it's already back in place.
  281. And now if I go facilities and refresh, that reappears.
  282. So that .htaccess is a set of conditions that my Apache
  283. web server understands from this section here.
  284. And it can follow those conditions.
  285. I've used those within Parkwood Vale's actual site as well, because when I go into
  286. facilities, you will see it doesn't end with .php.
  287. It is a .php file, but it's not there to be shown.
  288. KnowledgeMind isn't there yet, but there is that.
  289. So within this here, I'll put that back on the screen so you can see how it looks.
  290. this .htaccess hidden file has a bit of control to allow us
  291. to load our page and prettify our page.
  292. That if I didn't have these in place, and I save that a second,
  293. and I go back to localhost, I try to go to facilities, it doesn't
  294. exist. If I tell it .php, it will load my facilities page.
  295. But with that there and save it, my facilities.php will load and
  296. remove the PHP for me so it looks more friendly to human beings.
  297. Non-programmers don't really understand that there are different volumes, different
  298. types of internet files. And if they see anything other than .html or even .html,
  299. they'll probably panic. So by using this .htaccess, we can remove that and
  300. not have to worry about upsetting anybody.
  301. Now, let's just bring the slide show back up, leave that on the screen for a second,
  302. and see how we get on with that.
  303. While you're having a couple of seconds to look at that, I'm going to fix my mistake
  304. and put the second element in milliseconds.
  305. Hopefully this tutorial showing you how to create that interactive that
  306. image slider will be a lot simpler than our tutorial last
  307. week which I know is a very difficult one took over an hour well took over two hours
  308. to create.
  309. So that has allowed us to create our little JavaScript based image
  310. slider element.
  311. thank you for watching as usual any questions any issues do not hesitate to get in
  312. contact that's what i'm here for thank you