How Do I Execute A Script Defined In the Database?

I have a client who wants to have a chat function accessible on the booking form of my app.

If there is a database field script_html which contains the text:

<script id="xyz" async="true" src="abc"></script>

How am I best to take that text from a server action field like get_activity.data.script_html and have it actually execute on the page?

Here's a node solution.

In my layout:

Use Server Side Data to query the database for the scripts:
Screenshot 2024-11-12 at 5.59.44 AM

Where I want the scripts inserted (snippets.head_start is simply the output from the load_layout api)

<%-_('snippets.head_start')%>

Note the use of unescaped output ( <%- ) as provided by ejs templating:

Screenshot 2024-11-12 at 6.03.51 AM

2 Likes

Thanks Ken... but I'm PHP... :roll_eyes:

You'll need to run a Page Flow or something with the Run JavaScript step. There you will call a JavaScript function like "runEval":

<script>
function runEval(str) {
    eval(str);
}
</script>

Somehow you have to pass the string to it. I don't know if the above function works, but you get the idea

Edit: eval won't work for your particular example, ask ChatGPT how to inject an HTML code (coming from a variable) into the document body

@Teodor ...

Does Wappler support dmx-bind for a script tag?

<script id="xyz" async="true" dmx-bind:src="sc.data.html_script"></script>

Is this the same you needed some time ago?

1 Like

Thanks @teodor... you have a way better memory than me! :rofl: