Using Javascript 2

Even if you're not comfortable developing your Director projects with Javascript, the Javascript engine provides a few useful built-in objects and functions and can be used in Lingo.

1. JObjects

There are several javascript objects including Arrays, Dates, Numbers and Strings. These objects can be instantiated and returned to Lingo using a wrapper function like this:

-- Movie script (syntax javascript)

// Wrapper function for returning JObjects to Lingo
function GetDate (str) {
   return new Date(str);
}

Using this wrapper function, you can now access javascript object in Lingo:

today = GetDate()
put today
-- <Object jobject 2 3ed8d0>

In lingo, you can call the methods of this object - even passing Lingo data types in parameters (with some limitations):

put today.toString()
  -- "Tue Nov 28 2006 11:43:18 GMT+1100"
tomorrow = GetDate()
tomorrow.setDate(tomorrow.getDate()+1) 
put tomorrow.toString()
-- "Wed Nov 29 2006 11:45:58 GMT+1100"
bday = GetDate()
bday.setFullYear(1967,8,6)
put bDay.toString()
--"Wed Sep 06 1967 12:02:32 GMT+1000"

Note in the last example that months start as zero (January is month 0).

Limitations

Javascript seems to handle bigger numbers than Lingo. For example, if you call the getTime() method within a Javascript script, you will get the number of milliseconds between 1/1/1970 (GMT) and the current Date object. However, if you try this in a Lingo script, you will have problems because the result is bigger than Lingo's maxInteger.

Why Bother?

Lingo has its own Date object (the very useful systemDate) so you might be wondering why you might bother with the Javascript date object. One limitation of the Lingo object is that it does not measure milliseconds. With the Javascript objects, you can determine the elapsed time in milliseconds like this:

date1 = new Date("November 22 13:23:30 2006 ");
date2 = new Date("November 22 13:23:35 2006 ");
trace( date2.getTime() - date1.getTime());

Array objects don't really offer much over Lists, but string objects have some potentially useful methods especially relating to regular expressions (see this script for example)

2. Javascript Functions

Javascript also has a few useful 'global functions', such as the various functions for encoding data (escape, encodeURI and encodeURIComponent() - each of which comes with the corresponding unescape, decodeURI and dencodeURIComponent().

function EncodeData(str) {
  //note: will not encode: !@#$&*()=:/;?+'
  return encodeURI(str);
} 

function DecodeData(str) {
  return decodeURI(str);
} 

Lingo has its own (possibly undocumented) urlEncode() function. So which one should you use? The answer depends on what you want to uncode. Here's a quick comparion of the four methods:

-- "raw data = "!@#$%^&*(){}[]=:/;?+\'" & QUOTE & TAB & RETURN"
-- "urlencode: !%40%23$%25%5e%26*()%7b%7d%5b%5d%3d%3a%2f%3b%3f%2b%5c'%22%09%0a"
-- "encodeURIComponent: !%40%23%24%25%5E%26*()%7B%7D%5B%5D%3D%3A%2F%3B%3F%2B%5C'%22%09%0D"
-- "encodeURI: !@#$%25%5E&*()%7B%7D%5B%5D=:/;?+%5C'%22%09%0D"
-- "escape: %21@%23%24%25%5E%26*%28%29%7B%7D%5B%5D%3D%3A/%3B%3F+%5C%27%22%09%0D"
Last updated 28th November, 2006

© 2006 MeccaMedialight. Site Powered by Wrangler 8.