JS/AS Date Functions

A few date manipulation functions for JS/AS… Convert a MySQL DATE or DATETIME field into a valid JS/AS Date object:

UPDATE: Apparently FireFox can’t handle dashes when parsing the date. In the above replacement, use slashes instead:

Convert a JS Date Object to a MySQL formatted string

Get amount of time between […]

JS/AS Date Object from MySQL DATE or DATETIME

The ECMAScript Date object accepts a string that it pretty generously tries to convert to a valid date. However, DATE and DATETIME returns from MySQL are formatted Y-M-D, which will cause new Date(str) or Date.parse(str) to barf. The quick fix is just to use a simple RegExp to move the year to the 3rd position. […]

AS3 – Casting objects

There seems to be some confusion amongst the community as to the details of casting objects to a class. There are two ways to do this: 1. Cast using the constructor… Constructor(obj); 2. Cast using the “as” operator… (obj as Constuctor); There are subtle yet significant differences between these two approaches. The first – passing […]

JS/AS – Number to Dollar Format

little proto to convert a number to currency, with dollar sign, commas and 2 decimal places…

or procedurally…

AS3 – Detect if JS really is available

ExternalInterface.available returns if the swf is in an environment capable of providing an external scripting engine – it does not detect whether JS is disabled. Here’s a quicky little function that will:

AS3 FileReference Upload Path Relativity (Bug?)

This behavior (dare I say: bug?) also relates to relative pathing when using a Video object’s load() method. See my post on that here. I’ll repeat some of the information found there, here. In AS3, normally an external file is pathed relative to the housing document (.html or .php, whatever). When supplying the url property […]

AS3 – File Reference Bug

Strangely, a FileReference object declared as a local (function-scope) variable will fail to dispatch events. The file reference must be declared as a non-local persistent variable. Note the following will fail:

The file dialog will open, but the select handler will never be called. If, however, the FileReference reference is created outside the function […]

AS3/PHP – Quick-n-dirty AS3 Array to PHP

We commonly want to send an Array object from AS to PHP – while there are perfectly good, robust options available (AMFPHP, Serializer, etc), and obviously you could convert the Array to a string and explode it in PHP, that can be impractical if dealing with non-trusty data (like user input), since the delimiter might […]

AS3 Video.load path relative to .SWF

In AS3, normally an external file is pathed relative to the housing document (.html or .php, whatever). If you have the following directory structure: /root/docs/index.html /root/assets/swf/app.swf /root/assets/images/pic.jpg /root/assets/video/vid.flv and app.swf was housed in index.html, and you wanted to use a Loader to bring up pic.jpg, you’d use:

Note that the path is relative to […]

AS3 get DisplayList (recursive)

pass any DisplayObjectContainer to a new instance of DisplayList and get back an array of all descendants. [as3] package { import flash.display.DisplayObject; import flash.display.DisplayObjectContainer; public dynamic class DisplayList extends Array { public function DisplayList(target:DisplayObjectContainer):void { recurse(target); } private function recurse(target:DisplayObjectContainer):void { var qty:uint = target.numChildren; for (var i:int = 0; i < qty; i++) { […]