JS-151-API
  • JavaScript Library
  • 02-DOM-Manipulation
    • Overview
    • DOM Explained
    • Example Setup
    • Selecting Elements
    • Events
    • Complete Example
  • 03-API Fundamentals
    • 0-Getting Started
      • Welcome
    • 1-Intro-To-APIs
      • Intro
      • Client
      • Requests
      • JSON
      • API Endpoints
      • Server
      • Response
      • Statelessness
      • REST
    • 2-Asynchronous-Programming
      • Intro
      • Callbacks
      • Promises
      • Promises Continued
      • Chaining Promises
    • 3-Fetch
      • Fetch Intro
      • Star Wars
        • Star Wars API
        • Star Wars Setup
        • Star Wars JS
      • Random Photo
        • Unsplash
        • Unsplash Setup
        • Unsplash JS
    • 4-Apps
      • 01-New York Times
        • 00-App Intro
        • 01-HTML/CSS/API Key
        • 02-Variables
        • 03-Event Listeners
        • 04-fetchResults
          • 01-fetchResults()
          • 02-preventDefault()
          • 03-fetch() method
          • 04-Dates
        • 05-displayResults
          • 01-Logging JSON
          • 02-Working with JSON
          • 03-Link Display
          • 04-Results Navigation
          • 05-Results Update
          • 06-Keywords
          • 07-Images
        • 06-Pagination
          • 01-Pagination Intro
          • 02-Pagination Functions
        • 07-Next Steps
      • 02-YouTube
        • html
        • youtube.css
        • youtube.js
      • 03-GoogleMaps
        • Setup
        • HTML and CSS files
        • API Key
        • JS Setup
        • Adding Your Location
        • Listeners
        • Custom Options
Powered by GitBook
On this page
  • File Structure
  • HTML File
  • JavaScript
  1. 03-API Fundamentals
  2. 3-Fetch
  3. Random Photo

Unsplash Setup

Next, let's go ahead and set up our HTML file, and get our JS file started.

File Structure

We will be working with the following files:

    javascript-library
        └── 0-JavaScript-PreWork
        └── 1-JavaScript-Fundamentals
        └── 2-JavaScript-DOM-Manipulation
        └── 3-JavaScript-API-Fundamentals
            └── 1-Intro-to-APIs
            └── 2-Asynchronous-Programming
            └── 3-Fetch
               └── 01-Star-Wars
               └── 02-Unsplash
                 └── index.html <--- You will be working in these files
                 └── random-photo-fetch.js <---

HTML File

Let's start with our HTML file. Make your index.html look like the below code:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width">

    <title>Fetch: Random Picture</title>
  </head>

  <body>
    <h1>Random Picture</h1>
    <h3>Here we'll use the Unsplash API to grab a random picture every time we come to the page:</h3>
    <img src="" class="random-image">
    <script src="random-photo-fetch.js"></script>
  </body>
</html>

JavaScript

Now let's start our random-photo-fetch.js file. We'll start with document.querySelector() just like in the Star Wars API JS file. This is grabbing our .random-image img tag and saving it to the variable randomImage.

var randomImage = document.querySelector('.random-image'); 
console.log("randomImage:", randomImage);

Next, we'll dive into working with our API and fetch!

PreviousUnsplashNextUnsplash JS

Last updated 7 years ago