Calling function in in another java-script file from JS code

Not applicable

I'm creating a JS callout policy that will invoke code in a JavaScript file. Let's call it A.js.

How do I make a call to a function doSomething() in another JavaScript file called B.js?

Is there a way to 'include' or 'import' or 'require' B.js in A.Js?

Appreciate the help.

Solved Solved
2 5 1,808
1 ACCEPTED SOLUTION

Yes, there is.

When you define your Javascript policy, include B.JS in the IncludeURL as shown below

<Javascript async="false" continueOnError="false" enabled="true" timeLimit="100" 
        name="your-javascript-name">
    <DisplayName>JavaScript 1</DisplayName>
    <ResourceURL>jsc://A.js</ResourceURL>
    <IncludeURL>jsc://B.js</IncludeURL>
</Javascript>


The above would have the same semantics as if you "imported" B.js into A.js

Keep in mind that B.js needs to exist next to A.js or it needs to be uploaded as an organization level resource.

View solution in original post

5 REPLIES 5

Yes, there is.

When you define your Javascript policy, include B.JS in the IncludeURL as shown below

<Javascript async="false" continueOnError="false" enabled="true" timeLimit="100" 
        name="your-javascript-name">
    <DisplayName>JavaScript 1</DisplayName>
    <ResourceURL>jsc://A.js</ResourceURL>
    <IncludeURL>jsc://B.js</IncludeURL>
</Javascript>


The above would have the same semantics as if you "imported" B.js into A.js

Keep in mind that B.js needs to exist next to A.js or it needs to be uploaded as an organization level resource.

Thank you. I just figured it out and was posting the answer. You beat me to it. Appreciate the help.

See also, this article regarding structuring JavaScript modules in Apigee Edge.

A related question and answer here: Sharing a javascript file, across policies

Thanks. This is very useful.