A very very simple random quote generator made for the Free Code Camp project. Gets a random quote using a quote API and allows the user to tweet it.
A Pen by Tony Nyagah on CodePen.
| <head> | |
| <meta charset="UTF-8"> | |
| </head> | |
| <!-- bootstrap styling --> | |
| <div class="container-fluid"> | |
| <div class="row text-center"> | |
| <h2>Very Simple Random Quote Generator</h2> | |
| </div> | |
| <div class="row text-center"> | |
| <div class="col-xs-12 well message"> | |
| The quote will go here. | |
| </div> | |
| </div> | |
| <div class="row text-center"> | |
| <div class="col-xs-12"> | |
| <button id="getMessage" class="btn btn-primary"> | |
| Get Quote | |
| </button> | |
| <a href="http://google.com" class="btn btn-primary" id="tweetQuote" target="_blank">Tweet Quote</a> | |
| </div> | |
| </div> | |
| </div> |
A very very simple random quote generator made for the Free Code Camp project. Gets a random quote using a quote API and allows the user to tweet it.
A Pen by Tony Nyagah on CodePen.
| $(document).ready(function() { | |
| $("#getMessage").on("click", function() { | |
| /* | |
| // with getJSON | |
| $.getJSON("https://api.github.com/users/TonyNyagah", function(data) { | |
| $(".message").html(JSON.stringify(data)); | |
| }); | |
| */ | |
| /* | |
| // with $.ajax() | |
| $.ajax({ | |
| url: "https://api.github.com/users/TonyNyagah", | |
| dataType: "json", | |
| success: function(data){ | |
| $(".message").html(JSON.stringify(data)); | |
| } | |
| }); | |
| */ | |
| // now get a random quote from a site with HTTPS | |
| $.ajax({ | |
| url: "https://andruxnet-random-famous-quotes.p.mashape.com/", | |
| headers: { | |
| "X-Mashape-Key": "hoTnRMrQUkmshFojXahmqJXa7cl8p1OY1uBjsnwNtkRylnK9sh" | |
| }, | |
| method: "POST", | |
| contentType: "application/x-www-form-urlencoded", | |
| dataType: "json", | |
| success: function(data) { | |
| /* $(".message").html(JSON.stringify(data)); */ | |
| var html = ""; | |
| html += | |
| "<blockquote>" + | |
| data.quote + | |
| "<footer><cite>" + | |
| data.author + | |
| "</cite></footer></blockquote>"; | |
| $(".message").html(html); | |
| var encodedQuote = encodeURIComponent(data.quote); | |
| var tweetUrl = "https://twitter.com/intent/tweet?text=" + encodedQuote; | |
| $("a").attr("href", tweetUrl); | |
| /* | |
| console.log(data); | |
| console.log(encodedQuote); | |
| console.log(tweetUrl); | |
| */ | |
| } | |
| }); | |
| }); | |
| }); |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> |
| <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> |