How can I change this code so that I can add it to the standard document ready for jquery so that all my scripts are together.
/*
* Fetch RSS feed once page has finished loading.
*/
(function(url, callback) {
jQuery.ajax({
url: document.location.protocol + '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&callback=?&q=' + encodeURIComponent(url),
dataType: 'json',
success: function(data) {
callback(data.responseData.feed);
}
});
})('http://www.bet365.com/news/en/betting/sports/rss', function(feed){
var entries = feed.entries, content, publishDate;
for (var i = 0; i < entries.length; i++) {
publishDate = new Date(entries[i].publishedDate);
date = publishDate.getDate() + '/' + publishDate.getMonth() + '/' + publishDate.getFullYear();
content = truncateText((entries[i].contentSnippet) ? entries[i].contentSnippet : entries[i].content, 100);
jQuery('#rss > ul').append('<li><a href="' + entries[i].link + '" title=" ' + content + ' " target="_blank"><span> ' + date + '</span>' + entries[i].title + '</a></li>');
}
});
separate the functions.. it will good to use and understand
var my_callback = function(feed){ // Change to desired URL
var entries = feed.entries, content, publishDate;
for (var i = 0; i < entries.length; i++) {
publishDate = new Date(entries[i].publishedDate);
date = publishDate.getDate() + '/' + publishDate.getMonth() + '/' + publishDate.getFullYear();
content = truncateText((entries[i].contentSnippet) ? entries[i].contentSnippet : entries[i].content, 100);
jQuery('#rss > ul').append('<li><a href="' + entries[i].link + '" title=" ' + content + ' " target="_blank"><span> ' + date + '</span>' + entries[i].title + '</a></li>');
}
function make_ajax_call(url, callback) {
jQuery.ajax({
url: document.location.protocol + '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&callback=?&q=' + encodeURIComponent(url),
dataType: 'json',
success: function(data) {
callback(data.responseData.feed);
}
});
}
then
$(document).ready(function(){
make_ajax_call('http://www.bet365.com/news/en/betting/sports/rss',my_callback);
});
Just wrap it with:
$(function(){
// Your code here
});
Actually $
is an alias for $.ready
(and it's the same as $(document).ready
) if function is passed as argument
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.