HomeCustomise & extendLoad a script from an external site

Load a script from an external site

Host your JavaScript files externally and load them into your intranet through the Developer Framework.

Before you start

  • You need access to the Control Panel with permission to manage the Developer Framework.

Note: This approach is a sample, not officially supported by Interact Technical Support. Review and test it with your development team before use.

Overview

The Developer Framework is good for quick modifications in JavaScript, and you can also use it to build more complex functions that interact with multiple parts of the page or multiple systems.

JavaScript can become difficult to manage as it grows, especially when several developers work on the system, because there is only one place to enter code. One solution is to host your JavaScript files on another website, such as a CDN or your organisation's own public-facing website, and load them into your intranet pages through the Developer Framework.

The sample below appends a single file to the head of the page. Using an array or similar, you could load multiple files:

$(document).ready(function(){
  var script   = document.createElement("script");
  script.type  = "text/javascript";
  script.src   = "https://my.web.com/codefile.js";    // use this for linked script
  document.getElementsByTagName("head")[0].appendChild(script);
});

Advantages of this approach

  • Modular files — you can keep all code that implements a specific feature in a single file.
  • Developer separation — when each developer has their own source files, there is less risk of one developer overwriting another's changes.
  • Source control — because the files are hosted as physical files on another website, rather than saved to the Interact file system, you can include them in your source control system.
  • Deploying between environments — the usual way to copy code between environments such as sandbox and live is to copy and paste, which is prone to error. With this approach you load the file in the second environment for instant deployment.
  • Error correction — a small error in JavaScript, such as a misplaced bracket, can stop all the JavaScript on your site and effectively break Interact. Undoing the change can be difficult if the Developer Framework is not accessible. Hosting the code outside Interact makes it easier to remove the problem code and bring Interact back to life.
Section: Customise & extend