Add custom scripts

Add custom JavaScript code or HTML content before the <body> tag to each page in your portal.

For example, you can define custom scripts to be executed when each page in your portal:

  • Loads into the DOM using the onLoad JavaScript event.
  • Is navigated away from using the onUnload JavaScript event.

Your custom function must be defined as part of the portal.pageEventListeners in the global namespace (declared on the window variable).

Both the onLoad and onUnload events receive as their first parameters the current path of the page (/quickstart, for example). The onUnload function receives as its second parameter the return value from the onLoad call enabling context to be passed between the two events. Use onUnload to clean up the event listeners that are no longer required and perform other clean up activities.

For example:

<script>
window.portal = {};
window.portal.pageEventListeners = {
  onLoad: (path) => {
    if (path === '/quickstart') {
      // Change text content of first <p> element to something
      // else. (DOM must be loaded when onLoad is called)
      document.getElementsByTagName('p')[0].textContent =
          'Welcome to the quick start! Be sure to send us your feedback.';
      // print a custom message to the console every second while user is on
      // quickstart page.
      const interval =
          window.setInterval(() => console.log('Hello'), 1000);
      return interval;
    }
    return undefined;
  },
  onUnload: (path, contextReturnedFromOnLoad) => {
    if (contextReturnedFromOnLoad != null) {
      // Stop printing custom message to console every second.
      window.clearInterval(contextReturnedFromOnLoad)

    }
  },
};
</script>

To add custom JavaScript code to each page in your portal:

  1. Select Publish > Portals and select your portal.
  2. Select Settings in the drop-down menu in the top navigation bar.
  3. Click the Custom Scripts tab.
  4. In the Custom Scripts section, enter the custom JavaScript code in the text box.
    You can include multiple scripts.
    WARNING: Make sure to enclose your custom code in <script> and </script> tags. Invalid JavaScript code has the potential to impact the correct display of content or introduce security vulnerabilities across your entire site.
  5. Click Save.