This JavaScript code snippet helps you to create a random quote generator on a webpage. It generates random quotes and allows users to tweet or post them on Tumblr. It fetches quotes from a JSON file and dynamically updates the displayed quote and author. Perfect for inspiring and sharing wisdom!
You can use this code on your website to add an engaging “Random Quote Generator” feature. It provides fresh content to your visitors, encouraging longer stays and repeat visits. Plus, it’s easy to integrate and customize for a personalized touch.
How to Create Random Quote Generator Javascript
1. First of all, load the Font Awesome CSS (for icons) by adding the following CDN link into the head tag of your HTML document.
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" />
2. After that, create the HTML structure for your Quote Generator. You can copy and paste the following HTML code into your HTML file. This code sets up the basic layout of the generator, including the quote display area and buttons.
<div id="wraper"> <div id="quote-box"> <div class="quote-text"> <i class="fa fa-quote-left"></i> <span id="text"></span> <i class="fa fa-quote-right"></i> </div> <div class="quote-author">- <span id="author"></span> </div> <div class="buttons"> <a class="button" id="tweet-quote" title="Tweet this quote" target="_top"> <i class="fa fa-twitter"></i> </a> <a class="button" id="tumblr-quote" title="Post this quote on tumblr" target="_blank"> <i class="fa fa-tumblr"></i> </a> <button class="button" id="new-quote">New quote</button> </div> </div> <div class="footer"> <a href="https://codepen.io/arciell/">by Arciel</a> </div> </div>
3. Use the following CSS code or customize it to match your website’s design. This step ensures that your Quote Generator looks stylish and fits seamlessly with your site’s design.
@import url("https://fonts.googleapis.com/css?family=Raleway:400,500"); * { margin: 0; padding: 0; } div { position: relative; z-index: 2; } body { background-color: #333; color: #333; font-family: "Raleway", sans-serif; font-weigth: 400; display: flex; justify-content: center; align-items: center; height: 100vh; } .footer { width: 450px; text-align: center; margin: 15px auto 30px auto; color: #fff; } .footer a { font-weight: 500; text-decoration: none; color: #fff; } #quote-box { border-radius: 3px; position: relative; width: 500px; padding: 40px 50px; display: table; background-color: #fff; } #quote-box .quote-text { text-align: center; width: 450px; height: auto; clear: both; font-weight: 500; font-size: 2em; } #quote-box .quote-text i { margin-right: 0.4em; margin-left: 0.4em; } #quote-box .quote-author { width: 450px; height: auto; clear: both; padding-top: 20px; font-size: 1.2em; text-align: right; } #quote-box .buttons { width: 450px; margin: auto; display: block; } #quote-box .buttons .button { height: 38px; border: none; border-radius: 5%; color: #fff; background-color: #333; outline: none; font-size: 0.85em; padding: 8px 18px 6px 18px; margin-top: 20px; opacity: 1; cursor: pointer; } #quote-box .buttons .button:hover { opacity: 0.9; } #quote-box .buttons .button#tweet-quote, #quote-box .buttons .button#tumblr-quote { float: left; padding: 0; padding-top: 9px; padding-bottom: 1px; height: 30px; width: 40px; margin-right: 5px; text-align: center; align-items: center; border-radius: 15%; font-size: 1.2em; } #quote-box .buttons .button#new-quote { float: right; } @media (max-width: 600px) { .footer { width: 100vw; } #quote-box { width: 100vw; padding: 40px 0; } #quote-box .quote-text { width: 100vw; } #quote-box .quote-author { width: 100vw; } #quote-box .buttons { width: 100vw; } } @media (max-width: 300px) { body { height: auto; } .footer { margin: 0; padding: 0; } #quote-box { height: auto; margin: 0; padding: 0; } #quote-box .quote-text { margin: 0; padding: 0; } #quote-box .quote-author { margin: 0; padding: 0; } #quote-box .buttons { margin: 0; padding: 0; } #quote-box .buttons .button#tweet-quote { margin-right: 1px; } #quote-box .buttons .button#tumblr-quote { margin-right: 0; } #quote-box .buttons .button#new-quote { padding: 8px 14px 6px 14px; } }
4. Finally, add the following JavaScript code to your project. It fetches a list of quotes from a JSON file hosted on GitHub using the fetch
API. Make sure to replace the URL with your own JSON file or source of quotes if needed.
const projectName = "Random-quote-machine"; const colors = [ "#16a085", "#27ae60", "#2c3e50", "#f39c12", "#e74c3c", "#9b59b6", "#FB6964", "#342224", "#472E32", "#BDBB99", "#77B1A9", "#73A857", "#ff0000", "#00ff00", "#0000ff", "#ffff00", "#ff00ff", "#00ffff", "#ff8800", "#ff0088", "#00ff88", "#88ff00", "#0088ff", "#8800ff", ]; let currentQuote = ""; let currentAuthor = ""; let quotesData = []; function getQuotes() { return fetch( "https://gist.githubusercontent.com/camperbot/5a022b72e96c4c9585c32bf6a75f62d9/raw/e3c6895ce42069f0ee7e991229064f167fe8ccdc/quotes.json" ) .then((response) => response.json()) .then((data) => { quotesData = data.quotes; }); } function getRandomQuote() { return quotesData[Math.floor(Math.random() * quotesData.length)]; } function getQuote() { const randomQuote = getRandomQuote(); currentQuote = randomQuote.quote; currentAuthor = randomQuote.author; const tweetQuoteLink = document.getElementById("tweet-quote"); tweetQuoteLink.href = "https://twitter.com/intent/tweet?hashtags=quotes&related=freecodecamp&text=" + encodeURIComponent('"' + currentQuote + '" ' + currentAuthor); const tumblrQuoteLink = document.getElementById("tumblr-quote"); tumblrQuoteLink.href = "https://www.tumblr.com/widgets/share/tool?posttype=quote&tags=quotes,freecodecamp&caption=" + encodeURIComponent(currentAuthor) + "&content=" + encodeURIComponent(currentQuote) + "&canonicalUrl=https%3A%2F%2Fwww.tumblr.com%2Fbuttons&shareSource=tumblr_share_button"; const quoteText = document.querySelector(".quote-text"); const textElement = document.getElementById("text"); quoteText.style.opacity = 0; setTimeout(() => { quoteText.style.opacity = 1; textElement.textContent = randomQuote.quote; }, 500); const quoteAuthor = document.querySelector(".quote-author"); const authorElement = document.getElementById("author"); quoteAuthor.style.opacity = 0; setTimeout(() => { quoteAuthor.style.opacity = 1; authorElement.textContent = randomQuote.author; }, 500); const color = Math.floor(Math.random() * colors.length); const color2 = Math.floor(Math.random() * colors.length); document.documentElement.style.backgroundColor = colors[color]; document.body.style.color = colors[color]; const buttons = document.querySelectorAll(".button"); buttons.forEach((button) => { button.style.backgroundColor = colors[color]; }); } document.addEventListener("DOMContentLoaded", () => { getQuotes().then(() => { getQuote(); }); const newQuoteButton = document.getElementById("new-quote"); newQuoteButton.addEventListener("click", getQuote); });
That’s all! hopefully, you have successfully created a Random Quote Generator using JavasSript. If you have any questions or suggestions, feel free to comment below.
Similar Code Snippets:
I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.