Implement the random generator of famous quotes Based on JQuery and AJAX, jqueryajax
This is a small application I made when I first got started with AJAX. The main functions are as follows:
1. click the button to randomly generate a famous saying and its author name. If there is no author name, "Unknown" is displayed ".
2. click the button to share the famous saying on Twitter or Weibo.
HTML:
<div class="container-fluid text-center">
JQuery:
$(document).ready(function() { var quote, author; function getNewQuote() { $.ajax({ type: "get", url: "http://api.forismatic.com/api/1.0/", jsonp: 'jsonp', dataType: 'jsonp', data: { method: 'getQuote', lang: 'en', format: 'jsonp' }, success: function(response) { quote = response.quoteText; author = response.quoteAuthor; $('.quote').text('\"' + quote + '\"'); if (author) { $('.author').text('by ' + author); } else { $('.author').text('by Unknown'); } } }); } getNewQuote(); $('#change').on('click', function(event) { event.preventDefault(); getNewQuote(); }); $('#tweet').on('click', function(event) { event.preventDefault(); window.open('http://twitter.com/intent/tweet?text=' + encodeURIComponent(quote + ' by ' + author)); }); $('#weibo').on('click', function(event) { event.preventDefault(); window.open('http://v.t.sina.com.cn/share/share.php?title=' + encodeURIComponent(quote + ' by ' + author)); }) });
* Forismatic APIs can get famous quotes, but only the English and Russian versions... but there are also a lot of similar APIs in Chinese, and the implementation principle is similar.
Here is the DEMO. Welcome to FORK: Random Quote Generator.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.