Wednesday, October 31, 2007

JavaScript Date Object Tip

Quicktip: Nov. 31, 2007 == Dec. 1, 2007

Background
I had to repair a JavaScript based calendar popup tool today. For some reason November was not being displayed correctly. It had the correct number of days but November 1st was on Saturday, not Thursday.

This issue only manifested itself when the current date is the 31st day of the month and only caused display problem for months with less than 31 days.

What is going on?
This issue turned out to be related to an odd behavior of the JavaScript date object so I thought I'd share.

Let's go over the "offending" code:

var today = new Date(); // new Date(2007, 9, 31);
This creates a new date object with Oct. 31, 2007 (which is today)

today.setMonth(10);
This sets the month to November

alert(today);
This shows an alert box with Dec 1, 2007

But wait!! Didn't we set the month to November? Well, we did, but November only has 30 days. October had 31 days. So I guess we set the date to be Nov. 31. You could say that Nov. 31 is the same as Dec. 1. You might not say that but JavaScript does.


Solution(s):
1. Set the day of the month before setting the month.
2. Create a new date object using appropriate parameters: new Date(2007, 10, 1);

Saturday, October 27, 2007

SWFUpload has a new Home

SWFUpload has been offered a new home. After checking with the guys at mammon.se we've decided to accept a very gracious offer for hosting SWFUpload. Thanks Rob!

Come visit, chat, log bugs and ask questions at the new www.swfupload.org website.

The next little while will be a transition period and you'll probably see a bit of fluctuation on the site. Hopefully swfupload.org will become a great resource for SWFUpload developers.

Thanks for everyone's support!

Tuesday, October 23, 2007

SWFUpload Revision 7 beta 3

I decided I'd better get beta 3 out there. I've been too busy to test it and the demos as well as I'd have liked to so please forgive anything I've neglected. The updated demos and source have been posted in the usual place.

Changes
  • Fixed the Flash Version Detection. SWFUpload "should" properly detect Flash Player v9.0.28.
  • Fixed a left-over event call when cancelling an upload.
  • Fixed out of order parameters when calling uploadError.
  • Changed the showUI function and ui_function setting to swfUploadLoaded/swfupload_loaded_handler. This is more in line with other event callbacks.
  • Updated internal flashReady event so it is called like the other events (with setTimeout).
  • Added an internal event queue which fixes out of order events in Safari (out of order events were caused by using setTimeout to call them, just as I feared).

New Known Issues

If an upload is cancelled after the progress has reached 100% but before the upload script has finished processing then SWFUpload blocks until the upload script has completed. uploadComplete and fileComplete are still called along with uploadError. I'm still working out the best way to handle this situation (and have received some very good ideas in the comments).

To avoid this issue don't use the cancel feature or track your file progress in the JavaScript and prevent cancel calls to 100% still processing files. The issue will be fixed before the final 7.0 release.

You can test this by performing a long running process in your upload script. In PHP I tested by adding a sleep(30) statement. This causes SWFUpload to progress to 100% and then pause for 30 seconds while the upload script sleeps.

Question

I'm considering removing the ui_container and degraded_container settings. This would remove the last UI modifying bits in SWFUpload. A hands-off approach to the UI has been one of SWFUpload's design philosophies. I wondered what everyone else thinks about that.

Rather than remove them completely, however, I'd keep a default swfUploadLoaded handler that uses ui_container and degraded_container which could be stored in the customSettings object. That would keep a simple graceful degradation feature built in while better separating SWFUpload from the UI.

Monday, October 08, 2007

SWFUpload Revision 7.0 beta 2

Update: Revision 6.2 & 7 require Flash Player 9.0.28 or higher. However they fail to do proper checking to ensure that the necessary minimum Flash Player version is present. I will release an update to the R6 and R7 lines shortly.

Revision 7 beta 2

SWFUpload Revision 7.0 beta 2 is out!. I even updated the demos this time. It's all posted at the usual place: http://swfupload.praxion.co.za/. Many thanks again to our Host!

Don't forget to visit the Official SWFUpload site to show your support and to get some support.

Why Beta?

There is a bug in Flash Player that affects SWFUpload severely. It is more apparent in R7 because of the change in the Event design. I really like the new event design so I'm working hard to keep it. I'm not sure that this version is stable or how compatible it is with browsers on different operating systems. So it'll be Beta for a little while.

Changes in R7b2 from R7b1
  • I worked around an encoding bug in Flash Player where the Server Data or a file name or any string that contains a backslash (\) or a quotation mark (") in the wrong place will cause a JavaScript error which causes events to abort. Stupid Flash Player. See source for details.
  • I moved the ERROR_CODE_* constants in to "sub" objects so it's more clear what group the error belongs to (queue errors/upload errors). I changed all the error code numbers too. You didn't use any magic numbers, right?
  • Added the UPLOAD_STOPPED upload error which can supplement for an uploadStopped event.
  • Updated Flash calls and event calls with setTimeout to avoid limitations in Flash's ExternalInterface api. Stupid Flash. I haven't had any problems but watch out for concurrency and execution order issues.
  • Added a customSettings object to the SWFUpload instance so you can just set your own variables on an instance rather than using addSetting and getSetting (you can still use addSetting/getSetting if you want).
  • Changed the FILE_NOT_FOUND error code to FILE_ID_NOT_FOUND which is more precise.
  • Changed the parameter order on some events so the file object is always first in an effort to be more consistent.
Known Issues
  • You can run in to problems if, from JavaScript, you call in to Flash; which then calls back to JavaScript; who then tries to call back in to Flash. That final call in to Flash will fail. This is most apparent when calling startUpload() which calls back to uploadStart. If you try to call in to Flash from here, calling getStats() for instance, you may get an error or just a silent failure. I've work for 6 full days trying to understand and get around this. I've done pretty well by wrapping all the calls I could in setTimeouts but you can still force the bug so be careful. Stupid Flash.
  • Setting the file_post_name does not seem to work in the Linux Flash Player. It's an unnecessary feature anyway so don't use it unless you really have to (and you shouldn't have to). Thanks to Shadow Walker for the Linux bug reports.
  • You have to return something from your upload_url script or uploadComplete will never be called and your uploads will hang. I sometimes call echo ' ';
Where's the docs?

If anyone wants to write up some HTML format docs that'd be great. Otherwise I'll get to it eventually. In the meanwhile it's a good idea to skim through all the SWFUpload posts (the really old ones are still relevant). I've tried to include lots of comments in the code and between all the demos you can see how to do just about everything. Still, some docs would be nice.