Including Javascript Code

You can include Javascript code in your widget in the following ways:

Inline code

You can simply include in-line Javascript in your widget. It will work, but it is not recommended. If a widget containing in-line code is included several times on a page, then the code will be duplicated, making your pages slower to load and less efficient.

Use the jscripts folder

Put all your Javascript code in files in your webapp's jscripts folder. The Widget Framework will then merge all the files it finds in this folder (in alphabetical order) into a single file, together with any Javascript files in the WAR files your publication depends on. For detailed information about this process see Javascript Files.

Merging your files in this way does not cause any problems with interference between widgets. document.ready() and other events will all be handled as expected.

Use RequireJS

RequireJS is a Javascript module loader that manages dependencies and provides efficient on-demand loading of Javascript. If you want to use RequireJS to manage your Javascript code, then you should not put your Javascript files in your webapp's jscripts folder. You are free to use any other location (for example static/js). For further information about the use of RequireJS, see the RequireJS documentation.