Calling JavaScript from Morfik

From Morfikwiki.com

Jump to: navigation, search
Péter Illés
PannonRex Ltd.

Contents

[edit] Introduction

It 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.

[edit] Example

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.

[edit] Explanation

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…

[edit] See also

[edit] External Links

Personal tools