SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript "return" doesn't stop script

    I have a piece of js code that is giving me fits in IE8. I use it for a personal RSS feed on my site for members. It displays the headlines of whatever feed they have set. It is read from an XML cached file that is updated every 2 hours. If a feed messes up, it shows the error but continues to list the contents of the xml file all at once instead of stopping where the "return" is. Is this an IE bug or is there a way around it. Here is the snippet of code I'm using for this section.

    Code:
    // -------------------------------------------------------------------
    // initialize()- Initialize ticker method.
    // -Gets contents of RSS content and parse it using JavaScript DOM methods
    // -------------------------------------------------------------------
    
    rssticker_ajax.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 peice of an item inside a corresponding array
    for (var i=0; i<this.feeditems.length; i++){
    this.title[i]=this.feeditems[i].getElementsByTagName("title")[0].firstChild.nodeValue
    this.link[i]=this.feeditems[i].getElementsByTagName("link")[0].firstChild.nodeValue
    this.description[i]=this.feeditems[i].getElementsByTagName("description")[0].firstChild.nodeValue
    this.pubdate[i]=this.feeditems[i].getElementsByTagName("pubDate")[0].firstChild.nodeValue
    }
    document.getElementById(this.tickerid).onmouseover=function(){instanceOfTicker.mouseoverBol=1}
    document.getElementById(this.tickerid).onmouseout=function(){instanceOfTicker.mouseoverBol=0}
    this.rotatemsg()
    }
    }
    }
    The problem seems to be in the return right after the Error. I need it to stop from running the rest of the scripts if it encounters the error but continue if it doesn't. Can someone see what I am missing?

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    3 Thread(s)
    Maybe in IE8 you must end every statement (one command, usually one line of code) with a semi-colon (, which your script does not ?
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Here is that same code formatted to look better.
    You can see that the return is possible to occur only if the ajax communication was successful.

    Code javascript:
    // -------------------------------------------------------------------
    // initialize()- Initialize ticker method.
    // -Gets contents of RSS content and parse it using JavaScript DOM methods
    // -------------------------------------------------------------------
     
    var xmldata,
        instanceOfTicker;
    rssticker_ajax.prototype.initialize = function () {
        if (this.ajaxobj.readyState === 4) { //if request of file completed
            if (this.ajaxobj.status === 200) { //if request was successful
                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;
                }
                instanceOfTicker = this;
                this.feeditems = xmldata.getElementsByTagName("item");
                //Cycle through RSS XML object and store each peice of an item inside a corresponding array
                for (var i = 0; i < this.feeditems.length; i += 1) {
                    this.title[i] = this.feeditems[i].getElementsByTagName("title")[0].firstChild.nodeValue;
                    this.link[i] = this.feeditems[i].getElementsByTagName("link")[0].firstChild.nodeValue;
                    this.description[i] = this.feeditems[i].getElementsByTagName("description")[0].firstChild.nodeValue;
                    this.pubdate[i] = this.feeditems[i].getElementsByTagName("pubDate")[0].firstChild.nodeValue;
                }
                document.getElementById(this.tickerid).onmouseover = function () {
                    instanceOfTicker.mouseoverBol = 1;
                };
                document.getElementById(this.tickerid).onmouseout = function () {
                    instanceOfTicker.mouseoverBol = 0;
                };
                this.rotatemsg();
            }
        }
    };
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried adding the ; to the end of the return before and it still does the same thing. This gas me stumped as the only feed that seems to be doing it is my local paper online news one. It does it for a few days and then they fix it. I can send you the rest of the js file if you think that will help. It's a great little setup when it works right.

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    It seems that you don't want the rest of the code to be run if there's an error. If so, then you just need to place the rest of the code in an else clause.

    Code javascript:
    // -------------------------------------------------------------------
    // initialize()- Initialize ticker method.
    // -Gets contents of RSS content and parse it using JavaScript DOM methods
    // -------------------------------------------------------------------
     
    var xmldata,
        instanceOfTicker;
    rssticker_ajax.prototype.initialize = function () {
        if (this.ajaxobj.readyState === 4) { //if request of file completed
            if (this.ajaxobj.status === 200) { //if request was successful
                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;
                } else {
                    instanceOfTicker = this;
                    this.feeditems = xmldata.getElementsByTagName("item");
                    //Cycle through RSS XML object and store each peice of an item inside a corresponding array
                    for (var i = 0; i < this.feeditems.length; i += 1) {
                        this.title[i] = this.feeditems[i].getElementsByTagName("title")[0].firstChild.nodeValue;
                        this.link[i] = this.feeditems[i].getElementsByTagName("link")[0].firstChild.nodeValue;
                        this.description[i] = this.feeditems[i].getElementsByTagName("description")[0].firstChild.nodeValue;
                        this.pubdate[i] = this.feeditems[i].getElementsByTagName("pubDate")[0].firstChild.nodeValue;
                    }
                    document.getElementById(this.tickerid).onmouseover = function () {
                        instanceOfTicker.mouseoverBol = 1;
                    };
                    document.getElementById(this.tickerid).onmouseout = function () {
                        instanceOfTicker.mouseoverBol = 0;
                    };
                    this.rotatemsg();
                }
            }
        }
    };
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  6. #6
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Still doing thew same thing. The weird thing is tat the code I use in the header for it, only pulls the headlines. But the whole file is showing up on the page now, times, headlines and descriptions. I have a page and a half of code before the forums start. Luckily is it only doing that for me as I am the only one using the local online paper link. And it can be turned off in the member settings. But I would like to get the dang thing working right if possible so we don't have to worry about this in the future.

    What did you use to format that with, by the way? I don't see a way to format js in Rapid PHP.

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    I used a pretty print site for javascript at http://elfz.laacz.lv/beautify/
    A google search for one of the following will let you find suitable sites.
    • javascript pretty print
    • javascript beautify


    After that I made sure that the code didn't have any big issues in it with http://jslint.com/
    For best results select the button called "Good Parts" and enable "Assume a browser" before running JSLint.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  8. #8
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank, Paul. I will check those out and add them to my favorites. Looks like I will be needing them as I am going to be doing js for a while, at least until I get this figured out.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •