Morfik Tip: Calling JavaScript form Morfik Take #2
March 10, 2007It was almost a year ago that I wrote about calling JavaScript from Morfik (in Morfik Tips: How to integrate JavaScript into the client code). That method is still valid and especially suitable for larger bodies of JavaScript (like libraries), but there is now an easier way of doing this.
Let’s see an example for the new way:
function setOpacity(element:THTML_ElementExt; opacity: Integer); ['Obfuscate=false']; JavaScript; (*!
var object = element.style; object.opacity = (opacity / 100);
object.MozOpacity = (opacity / 100);
object.KhtmlOpacity = (opacity / 100);
object.filter = "alpha(opacity=" + opacity + ")";
*)
function getOpacity(element:THTML_ElementExt): Integer; ['Obfuscate=false']; JavaScript; (*!
var object = element.style;
return (object.opacity);
*)
You can simply copy the above function into your Pascal source file (browser side;-) and then you can call it passing the DOMHandle of any visual control together with an opacity value (in the range of 0-100) to set the element’s opacity, regardless of browser.
Let’s look at the syntax:
- The function declaration part can be pretty much the same as with any ordinary Morfik function (in the Pascal dialect you can use “function” instead of “procedure”, if you wish).
- Then we have the ['Obfuscate=false']; annotation that will direct the code generator to not obfuscate the function.
- Then we have the JavaScript; directive that tells the compiler that this is a JavaScript routine.
- Then comes the actual JavaScript, enclosed in the special comment tag (*! … *).
- You can return the result of the function with the standard JavaScript return statement.
It is very simple to call JavaScript this way from our code, but please keep in mind that the compiler cannot check the JavaScript code, so keep as much as possible in Pascal/Basic/C#/Java and use these techniques only when absolutely needed. Fortunately, as the Morfik Framework matures there are fewer and fewer cases when you have to…

Hello, Thanks for sharing this tip, I have tried it and
Marius | November 29, 2009Hello,
Thanks for sharing this tip, I have tried it and it works like a charm.
Can you perhaps comment on using Java Script inside Morfik when the Java Script represents something that is visual, like the calendar found at the link below.
http://www.softcomplex.com/products/tigra_calendar/
How would you integrate such a script within Morfik?
Regards
Marius