Hi there,
I need to add a simple script in the <head> tags of my Reactive application (ODC). How do I do this?
I think it's something very simple, but looking around the forum there does not seem to be a clear answer on it.
Yes. RequireScript is for loading external javascript files e.g. from a CDN. (https://mycdn.domain.com/mypackage/myscript.min.js)
If you have a downloaded script in your scripts folder there is even a third option to add it to a layout directly.
Hi Eric,
At on ready event of page use below javascript to add script in head.
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'url';
document.head.appendChild(script);
Best
Arun
Hi Arun,
Thank you for your response.
Sorry I'm really not well known with javascript, so where do I put exactly the script?
The script looks like this:
<!-- Start cookieyes banner --> <script id="cookieyes" type="text/javascript" src="https://...script.js"></script> <!-- End cookieyes banner -->
to include an external script to your application, you have two options
* Use the RequireScript client action from the System source (you need to add a reference to it).
RequireScript takes an URL as input and the script handles checking if the script is already required.
Depending on where you need the script you can place it in the OnInitialize event handler of an individual screen or in the Layout block you are using for your application. e.g. LayoutTopMenu.
* Use a Javascript node to inject a script. You would need that if you have additional parameters, e.g. loading the script as module. A sample JavaScript node script would look like this.
const existing = document.getElementById('stencilloader');if(!existing) { const stencilScript = document.createElement('script'); stencilScript.setAttribute('src','https://unpkg.com/os-aws-location@0.0.1/dist/os-aws-location/os-aws-location.esm.js'); stencilScript.setAttribute('type','module'); stencilScript.setAttribute('id','stencilloader'); document.head.appendChild(stencilScript);}
This script is taken from my article on how to use StencilJS web components in OutSystems. https://without.systems/using-ionic-stencil-components-in-outsystems-applications#heading-using-a-component-library-in-outsystems-applications
To clarify. Seeing you script you should use the RequireScript client action. You do not have extra parameters so you do not need the custom script approach.
Stefan
Hi Stefan,
Thank you for your reply!
Just to be sure: so with the RequireScript action I only have to add the url - which can be found in the script - as input? So I don't have to put the script itself anywhere else?
Great, thanks Stefan!