Clinical Decision Support - Using Lab Dates in JavaScript

This blog post was inspired by a question raised by Scantron (Michael) on the HEO iForms Google Group. He writes:

Hello all,

I was wondering if anyone is working with dates in java script? I have some criteria to order a CBC if one has not been completed in the past 24 hours. I can get it to subtract the dates and order the tests when I hard code the dates, but when using the @@patient.lab_result.Plt.date@@, the value brings in a carriage return symbol, and I get an unterminated string constant error. anyone have a solution for this?

My js:

var dt1 = new Date(Date.parse("@@patient.lab_result.Plt.date@@"));
var one_day=1000*60*60*24;
var time_diff1 = (parseInt(dt1.getTime()-dt2.getTime())/(one_day));

if (time_diff1 > 1 || isNaN(time_diff1)){
 $('#PtInr').val(1);
}

What comes in:

var dt1 = new Date(Date.parse("03/13/2013 09:46 AM
"));

The Issue

When opening an iForm in HEO, the parser takes two passes at your file. First, it reads the file and replaces all references in the file with their value. This includes things like BASE_URL and your @@ variables (including your Lab dates). Then, it takes the result of that process and sends it to the browser for rendering.

This is usually all fine and good, but the problem comes in that the mapped date comes with a carriage return at the end. If this were a string being passed back to your javascript function, it wouldn't be a problem since the carriage return would be part of the variable's value. But since it actually fowls up the structure of your code, it ends up throwing a fit and doesn't work.

So does that mean that we can't use the lab dates in our JavaScript functions? No, it doesn't mean that at all; you just have to get the information into your code in a different way, a way that having an extra line at the end doesn't matter.

The Fix

What you can do to bypass the issue is to put the @@ date variable in a DOM element on your page like a <DIV>, <SPAN> or <TD> and give it an ID that you can reference. If you want to use this information solely for scripting some action, then you can hide this particular element. If you're already planning to show it to the user, then it can serve a dual purpose. In this example, I'm using a <SPAN> element with "myLabDate" as the ID.

@@myLabDate@@

Then, in your javascript, you can reference the DOM object that contains your Lab Date information. Remember that it will still contain the newline at the end, so you may need to remove it, depending on how you're planning to use the information. Here are a couple snippets of code (one using jQuery and one using plain JavaScript) that shows some ways to do it:

// If you use jQuery
var myDate = $.trim($('#myLabDate').text());

// If you don't use jQuery
var myDate = document.getElementById('myLabDate').innerHTML.replace(/\n/g,'');

Once you've gotten the contents of that variable into a JavaScript variable, the world is your oyster and you can use that information in a variety of different ways. Leave a comment if you're doing something like this; I'd love to hear how people are using this lab data to make smarter forms, better processes, and improving patient safety.

Scott Morris is available for training, mentoring, troubleshooting, and iForms consulting. Find out more at www.thinkiforms.com

0 comments: