SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Thread: Undefined variable error

  1. #1
    SitePoint Member
    Join Date
    May 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Undefined variable error

    Hello, I have a Google Map which is being supplied with data from an XML file. I am having problems defining one of the elements to simpleDateFormat. In firebug and in the output in my infoboxes it says undefined. The output is defined and formats it properly. I am just having problem defining the parsed XML element to simpleDateFormat.

    The parsed input is from this XML which the element of question is <cap:expires>

    Code:
    <cap:expires>2013-05-02T19:00:00-05:00</cap:expires>
    From this XML file

    http://www.mesquiteweather.net/xml/s...nings_test.xml

    I have parsed it like this which gives me the new name expires.

    Code:
     var expires = nodeValue(entries[j].getElementsByTagName("cap:expires")[0]);
              if (!expires) expires = nodeValue(entries[j].getElementsByTagName("expires")[0]);

    Here is the simpleDateformat where I am trying to define expires.

    Code:
    function dateFromString(expires) {
    
      var bits = expires.split(/[-T:+]/g);
      var d = new Date(bits[0], bits[1]-1, bits[2]);
      d.setHours(bits[3], bits[4], bits[5]);
    
      // Get supplied time zone offset in minutes
      var offsetMinutes = bits[6] * 60 + Number(bits[7]);
      var sign = /\d\d-\d\d:\d\d$/.test(s)? '-' : '+';
    
      // Apply the sign
      offsetMinutes = 0 + (sign == '-'? -1 * offsetMinutes : offsetMinutes);
    
      // Apply offset and local timezone
      d.setMinutes(d.getMinutes() - offsetMinutes - d.getTimezoneOffset())
    
      // d is now a local time equivalent to the supplied time
    return d;
    
    } 
    
    var days = ["Sun","Mon","Tues","Wed","Thur","Fri","Sat"];
    var months =['Jan','Feb','March','April','May','June','July','Aug','Sept','Oct','Nov','Dec'];
    var ampm = " am";
    
    var dt = dateFromString(expires);
    var yr = dt.getFullYear();
    var mth = dt.getMonth();  // months in Javascript are 0-11 so May is month 4
    mth = months[mth];
    var dte = dt.getDate();
    var dy = dt.getDay();  // days are 0-6
    dy = days[dy];
    var hrs = dt.getHours();
    var h1 = hrs;
    var mins = dt.getMinutes();
    
    if (hrs >= 12) {ampm = " pm"}
    if (hrs >= 13) {hrs = hrs - 12}
    if (h1 == 0) {hrs = 12}
    
    if (hrs <10) {hrs = "0" + hrs};  // if  leading zero desired
    if (mins <10) {mins = "0" + mins};
    
    var dtstring = dy + ", " + mth + " " + dte + ", " + yr + " at " + hrs + ":" + mins + ampm;
    If you need to see the entire code you can from this link

    http://www.mesquiteweather.net/gmap/googlemap.js

    To see the result of the code with the map visit the link below. Mouse over a county and you see it says undefined.

    http://www.mesquiteweather.net/googlemap.html

    What am I doing wrong? I by no means know javascript at all. I have only started using it this past week only because I needed the use of Google Maps which uses Js.

    Any help would be greatly appreciated!

    -Thanks

  2. #2
    Grüße aus'm Pott
    SitePoint Award Recipient Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    2,370
    Mentioned
    38 Post(s)
    Tagged
    1 Thread(s)
    Hi,

    The error is being caused by line 180 of googlemaps.js:

    Code JavaScript:
    var expires = nodeValue(entries[j].getElementsByTagName("cap:expires")[0]);

    Where Chrome's DevTools is showing the error:

    Code:
    Uncaught ReferenceError: entries is not defined
    In line 82 you define entries thus:

    Code JavaScript:
    var entries = data.documentElement.getElementsByTagName("entry");

    However, searching through the source code of the page, I can find no mentions of the string "entry".

    Hope that helps a little
    Start a blog, they said. People will read it, they said.

  3. #3
    SitePoint Member
    Join Date
    May 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you, I am aware of that error. That was being thrown by some debugging I was doing. The error is actually at line 205 expires is not defined

    Code:
    var dt = (dateFromString(expires));
    Why I am getting that error I am not sure.

    The entries is the variable for the parent element in the XML file for entry.

    The strange thing is I can define expires directly to my infoboxes and it outputs the raw ISO 8601 format in the boxes.

    Then I can put a date string in the simpleDateFormat and define it to my infoboxes and it formats it without issues.

    Once I define expires with the simpleDateFormat so it can be feed the date strings from the parsed XML it says it's not defined even though it appears to be even though I can define it directly in my infboxes an I can output simpleDateFormat with a raw date string but when I put the two together it says it's not defined.

    I am not sure what I am doing wrong or what I am missing.

    -Thanks

  4. #4
    Grüße aus'm Pott
    SitePoint Award Recipient Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    2,370
    Mentioned
    38 Post(s)
    Tagged
    1 Thread(s)
    Hi,

    I just wanted to reply to this thread, but looked at your page and saw that you appear to have it sorted (in that the expires date is now displaying).
    Just for my personal curiosity, can you say how you fixed the error?
    Start a blog, they said. People will read it, they said.

  5. #5
    SitePoint Member
    Join Date
    May 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes sure, it was really easy and I knew it was but not knowing Js I was a little stuck. What it came down to was just adding this.


    Code:
    var dtstring = (dateFromString(expires));

    To this...

    Code:
    var expires = nodeValue(entries[j].getElementsByTagName("cap:expires")[0]);
              if (!expires) expires = nodeValue(entries[j].getElementsByTagName("expires")[0]);
    So all together it was like this.

    Code:
    var expires = nodeValue(entries[j].getElementsByTagName("cap:expires")[0]);
              if (!expires) expires = nodeValue(entries[j].getElementsByTagName("expires")[0]);
          var dtstring = (dateFromString(expires));
    That is why it was saying expires wasn't defined. Expires itself was defined but dstring wasn't defined. Once I defined that dstring variable it started working. Plus I had to make a couple other small changes with defining the var expires in the simpleDateformat.

    It's all working great now. Just trying to figure out how to add the date suffixes like 1st, 2nd, 3rd, 4th etc but that is more like a tweak than a problem.

    -Thanks

  6. #6
    Grüße aus'm Pott
    SitePoint Award Recipient Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    2,370
    Mentioned
    38 Post(s)
    Tagged
    1 Thread(s)
    Hi,

    Thanks for taking the time to follow up.
    For someone who claims to not know JavaScript, you seem to have debugged that like a boss!
    Start a blog, they said. People will read it, they said.

  7. #7
    SitePoint Member
    Join Date
    May 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    LoL, well thanks! I catch on and learn pretty fast but it took me a week to figure it out with a lot of researching the internet. Having the firebug tool really helps so I know exactly what the error is and where to look.

    You have any suggestions on how to add suffixes to the dates?

    -Thanks

  8. #8
    Grüße aus'm Pott
    SitePoint Award Recipient Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    2,370
    Mentioned
    38 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Texan78 View Post
    You have any suggestions on how to add suffixes to the dates?
    Oh yeah!

    Code JavaScript:
    function ordinal_suffix_of(i) {
        var j = i % 10;
        if (j == 1 && i != 11) {
            return i + "st";
        }
        if (j == 2 && i != 12) {
            return i + "nd";
        }
        if (j == 3 && i != 13) {
            return i + "rd";
        }
        return i + "th";
    }

    I can't claim the credit though. Lifted from Stack Overflow.
    Start a blog, they said. People will read it, they said.

  9. #9
    SitePoint Member
    Join Date
    May 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, how to I use that with my current time formatting? I am lost where you define it.

    -Thanks

  10. #10
    Grüße aus'm Pott
    SitePoint Award Recipient Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    2,370
    Mentioned
    38 Post(s)
    Tagged
    1 Thread(s)
    Hi,

    normally you would do:

    Code JavaScript:
    ordinal_suffix_of(5)
    => 5th

    So, you would have to apply it to dateFromString(expires) somehow.
    What does this actually return?
    Start a blog, they said. People will read it, they said.

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
  •