Lessons learned

December 25, 2006

Last week I was not able to post any entry in my blog, lots of work was done last week. Code refactoring, javascript validators, Smarty templates formatting are some major areas I worked on.

There was too many good things I learned. Firstly I took javascript validation part for my project, I found there were so much redudant code I written when I started working on the project. After taking some deep grasp on it, I was able to implement new validation function which does most kind of validation for me. This was the great achievement from last week. I wrapped my 1000 lines of code within just 100 lines, I am feeling very good about this. I share the code with some other team members and they were happy about the approch I took.

Smarty was one more things which was looking very egly no proper identetion, formatting, comments. No uniformity within the codes. I clean up most of the smarty templates, there are still some more which I will clean up this week.

In PHP, I clean up most part of PHP script, I feel very sorry that I didnt concetrated initially on my code which leads to this.

Some notes I took from this incident
-Always write good code, think before writing the code.
-Analyse it in every aspect, how it can help you in future.
-Is your code is easy to modify in future.
-Can I use the same code in other projects too.

I already started implementing this earlier but not on this project, that was mistake from my side.

But finally I am happy that I clean up many things last week.



Flex Builder crashes delete .snap file

December 15, 2006

It happen several times with us our flex builder suddenly crashes. Some time ago I come across the solution. If you ever have experince the problem flex builder crashing which give error message like following.
Flex error message
Remove .snap file from the development directory. At default location .snap file located at \.metadata\.plugins\org.eclipse.core.resources. Just remove .snap file and start your flex. Once drawback of doing this your project gets deleted from flex navigator you need to import it again.Snap file location

ExternalInterface to Make your flex methods Callable via HTML/Javascript

December 15, 2006

Its greate Experience working with addCallBack method and finally I got it working. I tried various ways. First I simply register the method in flex using addCallBack function but I didnt succeeded. After looking through live docs and reading more about it I come know swf initiates the and talks with browser that swf is ready to accept the call from HTML pages. How did I achieve this?

Here is my Mxml code

MXML Application:

<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml&#8221; layout=”absolute” creationComplete=”initApp();”>

import flash.external.*;
import mx.controls.Alert;
public var readyTimer:Timer;
public var javascriptMethodCounter:int = 0;

public function initApp():void
if (isContainerReady())
// If the container is ready, register the SWF’s functions.
/* If the container is not ready, set up a Timer to call the
container at 100ms intervals. Once the container responds that
it’s ready, the timer will be stopped. */
readyTimer = new Timer(100);
readyTimer.addEventListener(TimerEvent.TIMER, timerHandler);

public function showAlert(message:String):void
javascriptCounterText.text = javascriptMethodCounter + ” times javascript function called “;
javascriptMessageLogger.text = message;

public function timerHandler():void
if (isContainerReady())
// If the container is ready, register the SWF’s functions.

//setup the callback function so that HTML can call flex methods
public function setUpCall():void

//Checks wheather HTML page is loaded or not.
private function isContainerReady():Boolean
var result:Boolean = ExternalInterface.call(“isReady”);
Alert.show(“Result :”+result)
return result;
<mx:Panel x=”10″ y=”10″ width=”386″ height=”200″ layout=”absolute”>
<mx:TextArea x=”10″ y=”36″ width=”346″ height=”114″ id=”javascriptMessageLogger”/>
<mx:Label x=”10″ y=”10″ text=”times javascript function called” width=”260″



Above code comprises of following method :

initApp : This methods will get call on the creationComplete of an event. This checks wheather swf is ready for

to accept call from HTML/javascript or not.

isContainerReady: this simply call one javascript function which returns boolean value.

timerHandler: after calling isContainerReady function if result if false, we register a timer which gets call

every 100ms to register the swf communication. When successful timer get stoped and call setUpCall method.

setUpCall: Simply registers the Flex method with ExternalInterface addCallBack method.

showAlert: This is the method which we are registered to be callable via HTML

Controls :

javascriptMessageLogger : Displays the message passed via javascript method

javascriptCounterText: Shows the number of times methods get called from javascript.

HTML code:

<script language=”javascript”>
var interfaceObj;

function callFlexMethod()

interfaceObj = window[“ExternalInterface1”];
interfaceObj = document[“ExternalInterface1”];

// ——- Private vars ——-
var jsReady = false;

// ——- functions called by ActionScript ——-
// called to check if the page has initialized and JavaScript is available
function isReady() {
return jsReady;

// called by the onload event of the <body> tag
function pageInit() {
// Record that JavaScript is ready to go.
jsReady = true;


isReady : Simply returns the state of jsReady variable

pageInit: this method get called on onload event of page. This function makes jsReady true and get the copy of swf object into interfaceObj variable

callFlexMethod: Determines the browser type and get the proper object out of it

Form Element:

Enter Text : <input type=”text” id=”myText”><input type=”button” value=”Send to Flex” onclick=”interfaceObj.showAlert(document.getElementById(‘myText’).value);”>

myText: This is where we enter the message we want to passed to flex method.

Button : Onclick of button showAlert method of flex application get call with value from textInput.

I was able to successfully makes flex methods available to javascript and call it from javascript. One more thing it should be run from webserver.

Screenshot for the javascript flex communication for the above application

“for in” loop to print object properties with Javascript/Actionscript

December 14, 2006

Do you want to know object properties of any object? Its very easy listing all the properties and values with javascript/actionscript using “for in” looping structure.

Lets see how.


for(prop in object)


Above structure shows how to apply “for in” structure on object.

How it works?
-Its start collecting all properties name to the left side variable (prop) with in the bracket.


<script language=”Javascript”>
var obj = new Object(); //Create new object
obj[“firstName”]=”vijay”; //Assign property firstName with value vijay
obj[“lastName”]=”khambalkar”;//Assign property lastName with value Khambalkar
var str=””; //variable which will hold property values
for(prop in obj)
str+=prop + ” value :”+ obj[prop]+”\n”;//Concate prop and its value from object
alert(str); //Show all properties and its value

I used this technique many times while working with actionscript and javascript. This all me to see all properties and its individual value to track.

Coldfusion use cfexecute to execute server side executable

December 13, 2006

For Quite some time I was thinking of how we can execute any executables on the server. While browsing the net for coldfusion articles I am across cfexecute tag. So finally I got the tag which satisfy my needs.

We have some third party executables which converts our files into different format. But how to use this utility via web using coldfusion was question mark for us. Originally the idea was there in my mind about such kind of tag. Which I thought coldfusion must be offering, thats true it offered such tag. Coldfusion is Great!

We will like this.

-We will upload file on the server on to the specified url
-will use cfexecute command with script once the file is uploaded and will pass necessary arguments.

Cfexecute tag looks like

name = ” Application Name ”
arguments = “arguments”
outputFile = “Output file name”
timeout = “Timeout interval”>


name :
attributes require the application absolute path which we want to execute at the server side.

arguments :
arguments required by the application eg filename and output file format

outputFile :
This dump the response messages into specified file. eg. File is successfully converted.

This is the timelimit till the coldfusion server waits for the response from application.

You can find more information about the cfexecute at the livedocs.

How to Get Mouse Coordinates with Javascript !!!

December 11, 2006

Yesterday while working on a script and I wanted to know the cursor X, Y coordinates. Suprisingly I forgot how to get the X and Y coordinates of mouse. After thinking for some time, its strike that everything related to the current state is stored in events. So How to get the X and Y positions.

-capture mousemove event and list down all its properites

This is how I find X and Y coordinates

<title>Get Mouse Coordinates</title>
<script language=”javascript”>
var divObj;

* capture mousemove event, this statement will cause browser to
* call getMouseCoordites function each time mouse moves

*identify which event is supported
* Based on that collect pageX and pageY properties of the event object
* pageX and pageY gets the X and Y cursor coordinates
function getMouseCoordinates(event)
ev = event || window.event;
divObj.innerHTML = “Mouse X:”+ev.pageX + ” Mouse Y:”+ev.pageY;

//assign the mouseCoord Object to divObj
function loadDiv()
divObj = document.getElementById(“mouseCoord”);

<body onLoad=”loadDiv()”>
<div id=”mouseCoord”>Mouse Coordinates position will be displayed here.

Script works well with Mozilla, For IE need to do further modification in finding the exact position by calculating different properties value.

Calling Javascript function from Flex Application

December 6, 2006

There are various ways of calling javascript from your flex application ie. navigateToURL, ExtenalInterface. ExternalInterface is most elegent and simple way of calling javascript from flex application.

ExternalInterface API has also support browser checks. ExternalInterface class has static method call(javascriptFunctionName, ArgumentList, …) to which you can specify the method you need to call.

Success of the call method depends on the HTML pages with allowScriptAccess attribute which is within <object> or <embed> tag of HTML and it must be set to “always” or “sameDomain”.

Your typical flex Application with ExternalInterface will look like


<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml&#8221; layout=”absolute”>

import flash.external.*;
import mx.controls.Alert;

public function javascriptAlert():void
var javascriptFunction:String = “showAlert”;
var message:String = messageText.text;
ExternalInterface.call(javascriptFunction, message);

<mx:TextInput x=”10″ y=”20″ text=”Enter Message” id=”messageText”/>
<mx:Button x=”178″ y=”20″ label=”Call Javascript Function” click=”javascriptAlert()”/>


ExternalInterface.available property is to identify wheather your flash player supports ExternalInterface API or Not.

Javascript in HTML page

<script language=”javascript”>
function showAlert(message)
alert(“message from flex :”+ message );

You need to include this javascript into the HTML page.

In above example browser alert will be get displayed with the message you entered in the input box.