Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.

JavaScript global namespace

Avatar

Former Community Member

The namespace of a javascript defaults to the script object in which it is located, meaning that if I have a script object called "myScripts" with the following declaration:

function myFunction(XFAObject) {

// manipulate XFAObject...

return XFAObject;

}

, I need to precede a call with the name of the script object, i.e:

myScripts.myFunction( ... );

Is there a way to define javascript functions in the global namespace? This would help when importing external libraries (jlinq and such).

2 Replies

Avatar

Former Community Member

I just found the answer. In case anyone's interested, it seems you can pull the object into global namespace from within form1::initialize.

In the case of jLinq:

(function() {

     var global = (function(){return this;}).call();

     global.jLinq = jLinqScriptObject.jLinq;

})();

From now on, as soon as you need jLinq, you just call jLinq! For instance:

var users = [{first:"adam", age: 25},{first:"benny", age:32},{first:"caesar", age: 52}];

var results = jLinq.from(users)
    .startsWith("first", "a")
    .orderBy("age")
    .select();

Neat.

Avatar

Level 1

Hi c@tc.se, I found your post very interesting ince I was trying to do the same thing!

Anyway, I was NOT able to reproduce this behavior in ES2...

What I tried to do is:

  1. Create a new form
  2. Copy/paste above code in form1:initialize
  3. Copy/paste jLinq code in a script object
  4. Properly name the script object

Now i can't report di error log exactly since I'm not at work, but anyway, do you think it would be possible for you to submit a working source or a test PDF?

Thank you very much!!!