Created
February 2, 2018 22:08
-
-
Save nickihastings/5b1e9b4d3bb6c0dedc76d3206d5a9954 to your computer and use it in GitHub Desktop.
Random movie quote generator jquery ajax call to api and tweet button to tweet out quote.
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(){ | |
//get the quote api information with Ajax | |
getQuote(); | |
var bgCols = ['#2e67f7','#ea5c30','#38b582','#683a20','#59357d', '#555']; | |
function getQuote(){ | |
var num = Math.floor(Math.random()*6+1); | |
$.ajax({ | |
url: "https://andruxnet-random-famous-quotes.p.mashape.com/?cat=movies&count=1", | |
type: "GET", | |
data: {}, | |
dataType: "json", | |
beforeSend: function(xhr) { | |
xhr.setRequestHeader("X-Mashape-Authorization", "xYdGlgmLb6mshjBkNyNcbyQpGZuLp17o5c4jsnMmdf7VIAQut0"); // My Mashape key | |
}, | |
success: function (data){ | |
$('.quoteline').html(data.quote); | |
$('.author').html(data.author); | |
$('.twitter-share-button').attr("href",'https://twitter.com/intent/tweet?text='+ encodeURIComponent(data.quote) + encodeURIComponent(' Movie: ') + encodeURIComponent(data.author) + encodeURIComponent(' #FreeCodeCamp #RandomQuoteGenerator') + '&via=Nicki_Hastings'); | |
$('.container-box').css('background-color', bgCols[num]); | |
}, | |
error: function(err) { console.log("Failure!..."); console.log(err); } | |
}); | |
} | |
$("#nextQuote").on("click", function(e){ | |
e.preventDefault(); | |
getQuote(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment