Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>Random Ron Without Duplicates</h1>

<p>
  <img src="https://www.incimages.com/uploaded_files/image/1920x1080/parks-and-recreation_32716.jpg" alt="Ron Swanson, from the American sitcom 'Parks and Recreation', portrayed by Nick Offerman.">
</p>

<blockquote>
  <p id="quote" aria-live="polite">One moment while I find a quote...</p>
</blockquote>

<p>
  <button id="new-quote" type="button">More Ron</button>
</p>

              
            
!

CSS

              
                body {
  display: flex;
  flex-direction: column;
  justify-content: center;
  max-width: 30rem;
  padding: 1rem;
  margin: 0 auto;
  font-family: Georgia, serif;
  color: #222;
}

body > * + * {
  margin-top: 1rem;
}

h1 {
  color: #444;
}

h1::before {
  content: '💬 ';
}

blockquote {
  font-size: 1.5rem;
}

blockquote p::before {
  content: open-quote;
}

blockquote p::after {
  content: close-quote;
}

button {
  font-size: 1.25rem;
  font-family: Arial, sans-serif;
}

              
            
!

JS

              
                //
// Variables
//

// Get the placeholder for the quote
const placeholder = document.querySelector('#quote');

// Get the button for fetching new quotes
const button = document.querySelector('#new-quote');

// Store the API endpoint
const endpoint = 'https://ron-swanson-quotes.herokuapp.com/v2/quotes';

// Store the last 50 quotes
const pastQuotes = [];


//
// Functions
//

/**
 * Get the JSON data from a Fetch request
 * @param {Response} response The Response object
 * @returns {Promise} The JSON data or the rejected response
 */
function getJSON (response) {
  return response.ok ? response.json() : Promise.reject(response);
}

/**
 * Make sure the quote is valid and get a new one if necessary
 * @param {String[]} quotes An array of quotes
 * @returns {String} A quote
 */
function checkQuote (quotes) {
  // Get a new quote if necessary
  if (pastQuotes.includes(quotes[0])) {
    return fetch(endpoint).then(getJSON).then(checkQuote);
  }

  // Limit the past quotes to 50 quotes
  if (pastQuotes.length === 50) {
    pastQuotes.shift();
  }

  // Add this quote to the past quotes
  pastQuotes.push(quotes[0]);

  // Return the quote
  return quotes[0];
}

/**
 * Add a quote to the DOM
 * @param {String} quote The quote to be added
 */
function showQuote (quote) {
  placeholder.textContent = quote;
}

/**
 * Add an error message to the DOM
 */
function showError () {
  placeholder.textContent = 'I tried to find a quote, but something went wrong. Come back later, and be ready. Because I am. I was born ready. I\'m Ron F*cking Swanson.';
}

/**
 * Fetch a quote and add it to the DOM
 */
function fetchQuote () {
  fetch(endpoint)
    .then(getJSON)
    .then(checkQuote)
    .then(showQuote)
    .catch(showError);
}


//
// Inits & Event Listeners
//

// Show a quote when the page loads
fetchQuote();

// Show a new quote when the button is clicked
button.addEventListener('click', fetchQuote);

              
            
!
999px

Console