Loading JavaScript file(s) using HTTPService/URLLoader

In my last post, I talked about JavaScript Flex 2 component that can inject Javascript code in HTML wrapper's context. I experimented to see, if we can load Javascript files (.js) using HTTPService (or flash.net.URLLoader) in Flex2/AS3 projects and inject it.

Example Flex 2.0 code:



<![CDATA[
import flash.external.ExternalInterface;
import mx.events.*;
import mx.rpc.events.*;
import mx.rpc.http.HTTPService;
import com.abdulqabiz.utils.JavaScript;
private var javascript:JavaScript;
private var service:HTTPService;
private function onAppInit ():void
{
service = new HTTPService ();
service.url = "test.js";
service.useProxy = false;
service.resultFormat = "text";
service.addEventListener ("result", injectJavaScript);
service.send ();
}
private function injectJavaScript (event:ResultEvent):void
{
javascript = new JavaScript ();
javascript.source = String(event.result);
trace ("javascript injected: " + event.result);
}
private function invokeSayHelloWorld ():void
{
ExternalInterface.call ("sayHelloWorld");
}
private function invokeSaySomething (str:String):void
{
ExternalInterface.call ("saySomething", str);
}
]]>




Test.js used in example:

//test.js
var myName = "Abdul Qabiz";
function saySomething (str)
{
alert (str);
}
function sayHelloWorld ()
{
alert ("Hello World!");
}

You would need JavaScript.as (with proper package directory structure) and test.js (code posted above) in place to make above example to work.
I am thinking to load FABridge using this approach. I know, it's practically of no use except keeping code and logic at one place.

What makes you happy is undefined

Came up with this "What makes you happy is undefined", a few days back. I think, it makes sense.

We need to figure our own list of things, which make us happy.

BTW! I am very happy. These words are outcome of my nature of observing human-nature :)

JavaScript injection through ActionScript

In last post, I showed, how can we inject JavaScript using ActionScript or MXML into host HTML container/page.

One more use-cases, I can think of:

  • Request server-side script to send a JavaScript (for specific browser) as string.  Using JavaScript class, I can inject the javascript in HTML

Flex Component that injects Javascript in HTML host

I know subject line is confusing but I am not able to think of a better one.

I have written a Adobe Flex 2.0 component(JavaScript.as), which can be used as MXML tag in Flex 2.0 applications. You can write JavaScript code as text of this MXML tag. When your application loads, all that JavaScript code would be injected host HTML's DOM.

Confused? Let's look at some examples.

Sample MXML application using this component (test.mxml):-




<![CDATA[
var myName = "Abdul Qabiz";
function saySomething (str)
{
alert (str);
}
function sayHelloWorld ()
{
alert ("Hello World!");
}
]]>


<![CDATA[
import flash.external.ExternalInterface;
private function invokeSayHelloWorld ()
{
ExternalInterface.call ("sayHelloWorld");
}
]]>



HTML code that embeds test.swf(output of above code):-








You don't have Flash Player 9.
invoke saySomething ()
invoke sayHelloWorld ()
show myName

See an example Or Download (zip 133kb includes the SWF).

The code is available under the MIT license.

Disclaimer: I have only tested this in FireFox on Windows. So please drop a comment, if doesn't work in your system.

Update:
March 6, 2007 - Fixed a bug, which popped-up after I implemented comment removal feature. Thanks to Joan for point it out.
March 5, 2007 - Library has been updated, it can now handle comments in JavaScript code. It would basically remove all comments before injecting it in HTML container.

Things I want to see in Adobe Apollo

Lots of talk going on Apollo. I have kept quite because I had not seen Apollo except in MAX-2005 but today I saw screenshots on Ryan's blog. And I commented on his blog, I would like to see following things in Apollo:-

  • Cross platform and works on Mobile devices
  • Plugin architecture (That allows to execute natives DLLs with better platform detection APIs. A SDK would help)
  • Light weight (takes less memory and core-binary size is very smaller)
  • Better APIs for synchronization between offline/online apps (Nice wrappers around SharedObject, LocalConnection etc)
  • Access to OS level API with strong security sandbox
  • (X)HTML/CSS2/JavaScript support that comply with W3C and ECMA standards. This would allow you to deploy hybrid apps offline.
  • Backward compablity so that I can port existing (Flex 1.5, Flash Player 7/8) applications for Apollo.
  • Database connectors with ActionScript API, that would allow applications to connect DB directly without using middle layer.

Camera.getCamera (name) doesn't work in Adobe Flash Player 9 beta

I just noticed that if you have multiple cameras attached to your machine, in that case Camera.getCamera (cameraName) doesn't work Adobe Flash Player 9 beta 3. Camera.getCamera () works fine but that would return the default camera.
But if you want to use multiple cameras in your application, it seems you can't. I have informed Adobe Flash Player team about it and I am sure, it would get fixed.
However, I wanted to see if I am doing any thing wrong in following code. This is the sample code to reproduce the problem. Please let me know (through comments), if there is some problem in code itself. Thanks.


package
{
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.media.Camera;
import flash.media.Video;
import flash.events.*;
public class CameraExample extends Sprite
{
private var video1:Video;
private var video2:Video;
private var cameraWidth:int = 640;
private var cameraHeight:int = 480;
public function CameraExample()
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
var cameraNames:Array = Camera.names;
trace ("Available cameras: " + cameraNames);
var camera1:Camera = Camera.getCamera(cameraNames[0]);
var camera2:Camera = Camera.getCamera(cameraNames[1]);
if(camera1 != null)
{
camera1.addEventListener(ActivityEvent.ACTIVITY, activityHandler);
video1 = new Video(cameraWidth, cameraHeight);
video1.attachCamera(camera1);
addChild(video1);
}
else
{
trace("Camera1 not working");
}
if(camera2 != null)
{
video2 = new Video(cameraWidth, cameraHeight);
video2.x = cameraWidth + 20;
video2.attachCamera (camera2)
addChild(video2);
}
else
{
trace("Camera2 not working");
}
}
private function activityHandler(event:ActivityEvent):void
{
trace("activityHandler: " + event);
}
}
}

Workaround: FileReference onComplete is not fired on Mac OS

FileReference's onComplete event is not fired on Mac version of Adobe Flash Player 8.
We faced similar problem and found the workaround, thanks to comment by Bob on Nirav's blog.
There is simple workaround, you need to sent empty response from upload server-side script. In php, you would just do echo (""); after file-upload code. In ASP, you can do Response.Write (""). In Java servlet, you can do:

response.getWriter().println("");
response.getWriter().flush();

That's all, now onComplete event should trigger on Mac's Adobe Flash Player 8.
I am posting this with a specific subject line, so that developers can easily find by doing Google search.
Thanks to Bob and Nirav.

Thanks to Adobe ZMZ team

ZMZ stands for Zaphod, Mistral and Zorn. These are code names for Adobe Flash Player 9, Flex 2.0 and Flex Builder 2.0 respectively.
Adobe Flex 2 and Flash Player 9 would be released soon, not sure of dates. I can understand the excitement building up in the team. They must be waiting for RTM (release to manufacture/manufacturing), more than that, they would wait for the reactions from developer community and customers.
I am also very excited and very happy to think that ZMZ would be out finally after so much of hard work. Though, I am no more there (in Adobe) to celebrate that moment but guys I would celebrate whenever I would use the product. It's fruit of your hard work, dedication and passion.
I thank every member of ZMZ team for their hard work, which would make life of millions of developers easy over the time. I also thank every member of developer community (Flexcoders, MMbeta, flashcoders et al) for their continuous knowledge sharing efforts which at the end of day helps product team to build a better product.
BTW! This release would be party time for everyone not only ZMZ team. As these product would change the way we have been working to build Rich Internet applications and other types of applications. You already know about new features but you would soon see the innovative usages once product is released.

ActionScript 3 (AS3) information for C/C++ folks

I found this link on Kiwi blog about AS3 for C/C++ coders. If you are a C/C++ programmer, it might help you to get started with AS3.
Link: AS3 language 101 for C/C++ coders

Yaarana: a music video by IIT Kanpur students

</param></embed>
Reminds me of my school/college days.