Hi, thanks for using UIBUILDER.
You should note that allowing "random" code to be sent from the server to the client is, of course, something of a security minefield. For this reason, browsers intentionally make this hard to accomplish. And rightly so.
However, it IS possible to dynamically send code to the browser via UIBUILDER. The method is outlined here:
With more detail here:
You basically need to use UIBUILDER's low-code capabilities.
If you are ever getting that error, it will be for 1 of 2 reasons:
- The code you've added is executing BEFORE the uibuilder client library has executed.
- You are using modern JavaScript MODULES which self-isolate themselves.
If you are using Modules (AKA "ESM"), let me know and I can walk through how to use them correctly - this is a JavaScript thing by the way, not a uibuilder issue.
Assuming you are using the currently default IIFE version of the uibuilder client library and not ESM, you most probably haven't understood the impact of the defer
attribute in the default html code.
Any way, if you use uibuilder's built-in dynamic code loading capability, you should avoid the issues anyway. That's because, the client library will attach and execute the new code for you using standard browser API's.
To use that kind of approach - which I don't recommend - you would need that code to run after the uibuilder library has loaded. The easiest approach is to simply use the provided index.js
file for your code as that is already in the correct place in the index.html file and just needs uncommenting.
Then you would need to PARSE the transferred code. You cannot transfer certain things verbatim using http, websockets, or (as uibuilder uses) Socket.IO. Things like code and JavaScript Date objects can't be directly transferred. What happens is that those things are translated to strings (or simply removed). So to get code in the way you've started, you would need it to be a string on the Node-RED side and passed to an eval
function. You can read up on that function online and see why it is very much disliked by all.
I don't use eval in the client library, a more robust HTML API is used. Even so, you should be very cautious about passing code over and make certain that it cannot do harm to the client. You could, of course, do the same yourself - but it is complex and why bother when I've already done the hard work for you.