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>

<cap:expires>2013-05-02T19:00:00-05:00</cap:expires>

From this XML file

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

 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.

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

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

Hi,

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

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

Where Chrome’s DevTools is showing the error:

Uncaught ReferenceError: entries is not defined

In line 82 you define entries thus:

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

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

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

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?

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.

var dtstring = (dateFromString(expires));

To this…

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.

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

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! :slight_smile:

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

Oh yeah!

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.

Thanks, how to I use that with my current time formatting? I am lost where you define it.

-Thanks

Hi,

normally you would do:

ordinal_suffix_of(5)
=> 5th

So, you would have to apply it to dateFromString(expires) somehow.
What does this actually return?