/    Sign up×
Community /Pin to ProfileBookmark

error: function not defined. new to javascript and need help!

I am writing an app that displays content from an RSS feed. I got the code to start it off from a sample on the Web and I am modifying it. However the sample uses “prototype” and I have no idea how to add on to this. My latest error is trying to add a function called “next()” which will display the next RSS item. My browser is saying “next() is not defined” when I try to call next().

Here is my javascript for the next() function:

[CODE]
// ————————————————————–
// next() – Displays the next article
// ————————————————————–
rss_browser.prototype.next=function(){
this.rotatemsg();
}
[/CODE]

Here is my whole program which contains next() and rotatemsg().
Apologies for not being more specific, but I’m a newb and might miss including something important if I leave some code out.

[CODE]
function createAjaxObj(){
var httprequest=false
if (window.XMLHttpRequest){ // if Mozilla, Safari etc
httprequest=new XMLHttpRequest()
if (httprequest.overrideMimeType)
httprequest.overrideMimeType(‘text/xml’);
}
else if (window.ActiveXObject){ // if IE
try {
httprequest=new ActiveXObject(“Msxml2.XMLHTTP”);
}
catch (e){
try{
httprequest=new ActiveXObject(“Microsoft.XMLHTTP”);
}
catch (e){}
}
}
return httprequest;
}

// ——————————————————————-
// Main RSS Ticker Object function
// rss_browser(RSS_id, cachetime, divId, divClass, optionalswitch)
// ——————————————————————-

function rss_browser(RSS_id, cachetime, divId, divClass, optionalswitch){
this.RSS_id=RSS_id; //Array key indicating which RSS feed to display
this.cachetime=cachetime; //Time to cache feed, in minutes. 0=no cache.
this.tickerid=divId; //ID of ticker div to display information
this.logicswitch=(typeof optionalswitch!=”undefined”)? optionalswitch : -1;
this.pointer = 0;
this.ajaxobj=createAjaxObj();
document.write(‘<div id=”‘+divId+'” class=”‘+divClass+'”>Initializing ticker…</div>’);
this.getAjaxcontent();
}

// ——————————————————————-
// getAjaxcontent()- Makes asynchronous GET request to “rssfetch.php” with the supplied parameters
// ——————————————————————-

rss_browser.prototype.getAjaxcontent=function(){
if (this.ajaxobj){
var instanceOfTicker=this;
var parameters=”id=”+encodeURIComponent(this.RSS_id)+”&cachetime=”+this.cachetime+”&bustcache=”+new Date().getTime();
this.ajaxobj.onreadystatechange=function(){instanceOfTicker.initialize()};
this.ajaxobj.open(‘GET’, “retrieveRSS.php?”+parameters, true);
this.ajaxobj.send(null);
}
}

// ——————————————————————-
// initialize()- Initialize ticker method.
// -Gets contents of RSS content and parse it using JavaScript DOM methods
// ——————————————————————-

rss_browser.prototype.initialize=function(){
if (this.ajaxobj.readyState == 4){ //if request of file completed
if (this.ajaxobj.status==200){ //if request was successful
var xmldata=this.ajaxobj.responseXML;
if (xmldata.getElementsByTagName(“item”).length==0){ //if no <item> elements found in returned content
document.getElementById(this.tickerid).innerHTML=”<b>Error</b> fetching remote RSS feed!<br />”+this.ajaxobj.responseText;
return;
}
var instanceOfTicker=this;
this.feeditems=xmldata.getElementsByTagName(“item”);

//Cycle through RSS XML object and store each piece of the item element as an attribute of the element
for (var i=0; i<this.feeditems.length; i++){
this.feeditems[i].setAttribute(“ctitle”, this.feeditems[i].getElementsByTagName(“title”)[0].firstChild.nodeValue);
this.feeditems[i].setAttribute(“clink”, this.feeditems[i].getElementsByTagName(“link”)[0].firstChild.nodeValue);
this.feeditems[i].setAttribute(“cdescription”, this.feeditems[i].getElementsByTagName(“description”)[0].firstChild.nodeValue);
}
document.getElementById(this.tickerid).onmouseover=function(){instanceOfTicker.mouseoverBol=1};
document.getElementById(this.tickerid).onmouseout=function(){instanceOfTicker.mouseoverBol=0};
this.rotatemsg();
}
}
}

// ——————————————————————-
// rotatemsg()- Rotate through RSS messages and displays them
// ——————————————————————-

rss_browser.prototype.rotatemsg=function(){
var instanceOfTicker=this;
var tickerDiv=document.getElementById(this.tickerid);
var tickercontent='<a href=”‘+this.feeditems[this.pointer].getAttribute(“clink”)+'”>’+this.feeditems[this.pointer].getAttribute(“ctitle”)+'</a>’;
if (this.logicswitch==”showdescription”)
tickercontent+=”<br />”+this.feeditems[this.pointer].getAttribute(“cdescription”);
tickerDiv.innerHTML=tickercontent;
this.pointer=(this.pointer<this.feeditems.length-1)? this.pointer+1 : 0;
}

// ————————————————————–
// previous() – Displays the previous article
// ————————————————————–
function previous(){
alert(“previous clicked!”);
}

// ————————————————————–
// next() – Displays the next article
// ————————————————————–
rss_browser.prototype.next=function(){
this.rotatemsg();
}
[/CODE]

Please help! Thank you.

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@DaarklordauthorAug 27.2009 — I'm thinking it has to do with the fact that the sample code I used doesn't wait for user input to go to the next article, so it is just one continuous process. Mine handles the event of the user clicking next, but by that time the instance of rss_browser with all its variables has disappeared.

I worked around this by adding an onclick handler to the initialize() function instead of calling next() from the HTML page.

Code from the [CODE]rss_browser.prototype.initialize=function(){[/CODE] part of my code has this added to it:
[CODE]
document.getElementById("artNavNext").onclick=function(){
instanceOfTicker.rotatemsg()};
[/CODE]


And it's working fine.

Now I just need to define a rotatemsgback() function for going backwards. I don't know what to do here. Should I use rss_browser.prototype or just define the function as usual?
×

Success!

Help @Daarklord spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...