SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Feb 2010
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Undefined issues

    Hi

    I'm hoping for a little bit of help. I'm bringing in a CSV file via Javascript/AJAX and then attempting to create an unordered list.

    The AJAX bit works just fine, however building the list is proving an issue. Below is the relevant code:

    Code JavaScript:
    unction makeList() {
        if (xmlhttp.readyState==4) { 
            if (xmlhttp.status==200) { 
                var tmpArr=xmlhttp.responseText.split('\n');
                var out='<ul>';
                var tmp;
                var val;
                var txt;
                for (var idx=0;idx<tmpArr.length;idx++) {
                    tmp=tmpArr[idx].split(',');
    				txt = tmp[1];
                    val = tmp[0];
    				if(txt.charAt(0)=="I") {
    				out += '<li><a href="#">'+txt+'</a></li>';
    				}else{
    					//do nothing. Just in place whilst testing
    				}
                }
                out += '</ul>';
                document.getElementById('prodMenuItem').innerHTML=out;
            }
        }
     
    }

    As you can see I am attempting to bring out items from the CSV file that begin with "I". Everytime I do this I get:

    Code JavaScript:
    txt is undefined

    which errors out at the line

    Code JavaScript:
    if(txt.charAt(0)=="I") {

    But bizarrely if I do:

    Code JavaScript:
    alert(txt);

    AFTER the line my debugger says the code breaks at, then all the 'I's do indeed get alerted out to the screen.

    If anyone has any idea whats going on and how to approach fixing this I'd be very grateful

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,081
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    It would appear tmp[1] is undefined, so assigning it to txt makes txt also undefined.

    Can you give an example responseText ?
    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
    SitePoint Member
    Join Date
    Feb 2010
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi thanks for your help.

    I don;t understand how a var can be undefined and yet, as I said, when called to an alert box, display the right set of results.

    I tried putting in a set of

    Code JavaScript:
    if(txt == undefined){
    alert("arrgghhhh");
    }

    at varying points and can see that it gains definition right before the line

    Code JavaScript:
    txt = tmp[1];

    But then my debugger (venkman) swears that it (txt) is not defined and I get no results at all.

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,081
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    -- text removed, double post... --
    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

  5. #5
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,081
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    And when you try by converting al txt to strings, like

    Code JavaScript:
     if(String(txt).charAt(0)=="I") {
       // ...
     }

    Does that work?
    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

  6. #6
    SitePoint Member
    Join Date
    Feb 2010
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats got it - thanks very much


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
  •