Working with JavaScript

Working with JavaScript

Working with JavaScript

IntraWeb makes extensive use of JavaScript. All JavaScript files are contained in a resource file, linked to every IntraWeb application. The resource file is named IWData.res and is located inside the IntraWeb lib folder. This file also contains other resources used by JavaScript, like images.

Internal JavaScript files

Starting in IntraWeb 14.0.13, there are 2 sets of JavaScript files: The default set contains minified JavaScript files. When using these files, you will have better performance and lower bandwidth usage because they are smaller than the uncompressed files. They are also concatenated, so there are fewer requests to the IntraWeb application during session startup.

How IntraWeb serves internal JavaScript files

When IntraWeb renders a new IWForm, all the required JavaScript files are included in the HTML, like this:

<script type=”text/javascript” src=”/$/js/IWBase__EC971C3CB.js“> </script>

There is a random suffix at the end of the JavaScript file name. This is used to avoid caching issues when these files are updated. Note the command to serve internal JavaScript files: /$/js/. Every URL containing /$/js/ is telling IntraWeb that a internal JavaScript file is being requested. This way, this command is private to IntraWeb and it should not be used for anything else.

All those internal JavaScript files are kept in an internal list in memory and IntraWeb application does not neet to read the file from the disk. This greatly improves performance when serving these files.

Switching the file set

Also starting in IntraWeb 14.0.13, you may choose which JavaScript file set your application will serve. There is a property in TIWServerController class: JavaScriptOptions:

  • UseUncompressedFiles: If true, IntraWeb will use uncompressed JavaScript files. If false, compressed JavaScript files will be used. The default value is false. You may switch this to True, to make your application render and serve the uncompressed files. This way it is much easier to debug the JavaScript if you need to.
  • Debug: If true, the browser console window will be used to output some debug information already coded in the JavaScript files. The debug information helps to understand and follow what the JavaScript is doing.

Using your own JavaScript library

You may add your own JavaScript file to be served by IntraWeb, just like the internal JavaScript library. To do this, you should call the method Add() of the internal file list:

procedure TIWServerInternalFiles.Add(aResName, aFileName: string);

Where:

  • aResName: Name of the resource, linked to your executable, containing the JavaScript file
  • aFileName: Name of the JavaScript. This is the name used when rendering the HTML containing it.

Example:

gInternalFiles.Add(‘IW_JS_IWTree’, ‘/js/IWTree.js’);

How to:

  • Create a JavaScript file and compile it as a resource. The resource name must start with the IW_JS_ prefix. So, if you want to have a MyLib.js file in your pages, you need to create a resource named IW_JS_MyLib
  • Link the resource file to your project, using the $R compiler directive, like {$R MyLib.res}
  • Add it to the internal file list. You have to declare IWServerInternalFiles unit in the uses clause, and use the gInternalFiles var:

gInternalFiles.Add(‘IW_JS_MyLib’, ‘/js/MyLib.js’);

  • Now you may use it in your own HTML. For instance, if you need this file rendered in a specific form you may use:

procedure TIWForm.IWAppFormRender(Sender: TObject);
begin
Self.PageContext.ScriptFiles.Add(‘/js/IWTree.js’);
end;

  • The rendered HTML output will then contain something like that:

<script type=”text/javascript” src=”/$/js/MyLib__EC971C3CB.js“> </script>

See also

TIWServerController class reference