jquery - Run function on page load (Refresh RSS feed with new URL) Javascript -
i have app want display different rss feeds on 1 page, when corresponding team clicked.
for example, when click "aberdeen" want page show rss aberdeen. when click "celtic", want same page display rss celtic.
i attach code in 1 section - when page #feed
loads, need run getfeed()
function.
i using jquery mobile.
html "feed" page
<div data-role="page" id="feed"> <div data-role="header"> <h1 id="fheader">header</h1> </div> <div data-role="content"> <a href="#" data-role="button" data-icon="plus">add teams</a> <ul data-role="listview" class="list" data-inset="true" > </ul> </div> </div>
function getfeed
var feedurl="http://www.football365.com/arsenal/rss"; var n = 12; function getfeed(url, success, n){ url= feedurl.replace("??", n.tostring()); if(window.navigator.online) { $.jgfeed(url, function(feeds) { // check errors if(!feeds){ // there error return; } else { localstorage.setitem(url, json.stringify(feeds)); success(feeds.title, feeds.entries); } },n ); } else { // fall-back... var feed = json.parse(localstorage.getitem(url)); if(feed && feed.length > 0) { success(feed.title, feed.entries); } } } $(document).on('pagebeforeshow',function(){ getfeed(feedurl,function(title,items){ $("#title").text(title); for(var index=0; index<items.length; index+=1){ $("#list").append(formatitem(items[index])); } $("#list").listview('refresh'); },20); } );
function find rss url based on team
function getindex(teamlist, teamname){ for(var = 0; i<teamlist.length; i++){ if(teamlist[i].teamname == teamname){ return i; // if found index return } } return -1; // if not found -1 return } var = getindex(teamlist,name); // give 0 console.log(a); feedurl = teamlist[a].rss_url; console.log(feedurl);
as can see, rss url specific team found via index in array. once has found array, assigns feedurl variable. part works fine
however, when page opens, not take consideration change in variable. need refresh or rerun getfeed() function take account new value of feedurl.
hope clears things :) :)
if want run function when page has loaded use:
$(document).ready(function(){ getfeed(); };
this run function when page has rendered , #feed element available.