A Pen by Odysseas Samaras on CodePen.
Last active
June 28, 2017 15:35
-
-
Save odysseas/736202d8807a05a0cb9f to your computer and use it in GitHub Desktop.
Random Quote Generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<main> | |
<div class="container"> | |
<div class="page-header"> | |
<h1>Random Quote Generator<small> Using Forismatic API</small</h1> | |
</div> | |
<div class="well"> | |
<blockquote> | |
<p id="quote"> | |
</p> | |
<footer id="author"> | |
</footer> | |
</blockquote> | |
<div id="twitter-button"></div> | |
</div> | |
<button id="new-quote-btn" class="btn btn-primary btn-lg">New Quote</button> | |
</div> | |
</main> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function() { | |
getQuote(); | |
$("#new-quote-btn").click(getQuote); | |
twttr.widgets.load(); | |
}); | |
function getQuote() { | |
$.ajax({ | |
type: "POST", | |
crossDomain: true, | |
url: "http://api.forismatic.com/api/1.0/", | |
data: { | |
method: "getQuote", | |
format: "jsonp", | |
lang: "en" | |
}, | |
dataType: "jsonp", | |
jsonp: "jsonp", | |
jsonpCallback: "parseQuote" | |
}); | |
} | |
function parseQuote(response) { | |
$('#quote').text(response.quoteText); | |
$('#author').text(response.quoteAuthor); | |
var text = "'" + response.quoteText + "' - " + response.quoteAuthor; | |
$('#twitter-button').empty(); | |
twttr.widgets.createShareButton( | |
'http://codepen.io/odysseas/full/JYjZMm/', | |
document.getElementById('twitter-button'), { | |
text: text, | |
} | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> | |
<script src="https://platform.twitter.com/widgets.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's an awesome little code! Could you just explain why the value "jsonp" for json property ? that is
jsonp : "jsonp"
Does the site refer to this as the parameter for the "?callback=xyz" ? where in, instead of "callback", you have provided "jsonp"
Because, now, when I check the API in the forismatic site, it doesn't say much. It shows quotes instead, in the jsonp api page.