I have a standard piece of javascript that I am writing to establish a framework for all applications. We are embedding our forms into an iframe on a separate site, and part of that framework is the communication between parent and client. I've developed this and it's mostly working well.
Because of a whole host of reasons, I do not want the framework javascript to be embedded into each form, I would much rather I have a single file hosted by the parent. The parent is on www.mysite.awesome and volt is on forms.mysite.awesome so I won't get any CORS issues.
I can see that Volt supports this. Rather than uploading a file to volt, I can use a URL and include the Javascript from a central location.
However, when I load this in via a URL, the javascript behaves differently than it does when I have it embedded. It does not appear to have access to the NitroApplication.
This exact same code works just fine when I embed the javascript into the form, this only occurs when I load the javascript in over http.
I suspect this might be by design, but I'd like confirmation.
And if that is the case, I'd also like some advice on how best to centralize? I really want to have a single central source for our javascript - I do not relish the prospect of having to update all our forms whenever there is a change!
Your centralization approach is sound. You should not need the "window." in either case, just reference NitroApplication directly.
Hi, apologies about the delay, I was on annual leave. I'm back now and given this a solid second attempt.
I'm still having the issue, and for the sake of ease, I've vastly simplified my setup to the bare minimum to demonstrate this.
So what I've done is to create a very spartan JS file. At it's core, all it does is a simple alert.
I've put this JS file in a location that is both served by my local IIS, but is also accessible as a file. This means I can add it either as a file or serve it over http without any changes.
I've then created a form in HCL, and the only thing on that form is a button which calls the JS above.
Then I add my JS, and deploy and fire it up. The result I get varies based on if I embedded the JS or if I am serving it over http.
My JS is definitely loading and executing, I can see it in the console. Screenshots follow.
The screenshot below shows what happens when I embed the JS.
The screenshow below shows what happens when I load the exact same JS over http
I've extracted the form as a .volt and the .js I'm using, and have attached these. I've renamed them as .txt to allow that to happen.
Any input greatly appreciated!
volttest.js.txt
I agree with you that the default behavior could be simpler.
Referencing files by URL is only supported if the VoltConfig setting secureJS is enabled with a value of 'false'
It is important to understand that how the javaScript functions get loaded changes depending on if they are attached or referenced externally. For this reason, I would actually recommend that you just define your functions in your own object or globally. For example, your function becomes:
sayHello = function()
{
. . .
}
By re-defining the function in this way, it loads fine.
I can see no advantage to trying to define your functions within our 'sharedData' object.
I have filed a dev task to investigate how we can improve the default behavior, but this is something that we will work on for a future release.
I believe that you can move forward with your approach of including external js files that contain function definitions outside of the Volt Shared space.
Please let us know if you have any questions or concerns.
Thank you for your valuable feedback!