Can you include a Javascript in another Javascript

Not applicable

Hi, I am trying to find out if apigee allows JS to include/import another JS, ideally, JS that are loaded at Org or Env level as a ResourceFile

Solved Solved
1 2 4,702
1 ACCEPTED SOLUTION

Yes, you can! You can reference any resource file, which is either uploaded at Org level or Environment level. You refer the script under <ResourceURL> tag.

Eg. Here you are referring to a resource file named pathSetter.js.

<Javascript name='PathSetterPolicy' timeLimit='200'>
    <ResourceURL>jsc://pathSetter.js</ResourceURL>
</Javascript>

More examples are available here.

If you want to import/include a JS library (like Crypto) in your JavaScript file, then you use the <IncludeURL> tag.

Eg -

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Javascript async="false" 
        continueOnError="false" enabled="true" timeLimit="200" 
        name="JavaScript-1">
    <DisplayName>JavaScript 1</DisplayName>
    <Properties>
        <Property name="propName">propertyValue</Property>
    </Properties>
    <IncludeURL>jsc://javascript-library-file.js</IncludeURL>
    <ResourceURL>jsc://my-javascript-source-file.js</ResourceURL>

</Javascript>

You can use multiple IncludeURL elements. All of the modules specified in "IncludeURL" elements will be concatenated and pre-pended to your source file.

Thanks, Sudhee

View solution in original post

2 REPLIES 2

Yes, you can! You can reference any resource file, which is either uploaded at Org level or Environment level. You refer the script under <ResourceURL> tag.

Eg. Here you are referring to a resource file named pathSetter.js.

<Javascript name='PathSetterPolicy' timeLimit='200'>
    <ResourceURL>jsc://pathSetter.js</ResourceURL>
</Javascript>

More examples are available here.

If you want to import/include a JS library (like Crypto) in your JavaScript file, then you use the <IncludeURL> tag.

Eg -

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Javascript async="false" 
        continueOnError="false" enabled="true" timeLimit="200" 
        name="JavaScript-1">
    <DisplayName>JavaScript 1</DisplayName>
    <Properties>
        <Property name="propName">propertyValue</Property>
    </Properties>
    <IncludeURL>jsc://javascript-library-file.js</IncludeURL>
    <ResourceURL>jsc://my-javascript-source-file.js</ResourceURL>

</Javascript>

You can use multiple IncludeURL elements. All of the modules specified in "IncludeURL" elements will be concatenated and pre-pended to your source file.

Thanks, Sudhee

Sorry, I meant in the .js file, can another .js be imported or included to avoid to always define dependencies in the Policy file (as shown above). Thx