OK - So first off i’m a noob to wix. So please be gentle.
Here is what I am trying to do. I own a food truck / Food delivery service. I want to post nutritional data for my dishes on my website. I use a database food analysis service - recipal.com. They make FDA compliant labels available dynamically with a js embed code.
I have created a collection on my site with the links to the recipal data and the dishes. I am trying to send the js to the HTML element dynamically. The user will select a dish from a drop down box - and the label will be displayed.
On the On_Change event in the drop down I have this code:
export function dropdown1_change(event) {
//Add your code for this event here:
wixData.query('MenuItems')
.contains('recipe__name', $w('#dropdown1').value)
.find() // Run the query
.then(res => {
let DataRow = res.items;
let strURL = "<script src='https://www.recipal.com/recipes/" + res.items[0].recipe__embed_token + "/embed.js' type='text/javascript'></script>";
$w("#html3").postMessage(strURL);
});
This code takes the dish name, and queries the value of the embed token from recipal and builds the src value of the src tag in the js.
In the HTML element I have this code:
<html>
<head>
<style>
.button {
background-color: #155DE9;
color: white;
border: none;
padding: 10px 20px;
text-align: center;
font-family: 'Arial';
}
.label {
font-family: 'Arial';
}
.input {
font-size: 14px;
}
</style>
<script type="text/javascript">
// when a message is received from the page code
window.onmessage = (event) => {
if (event.data) {
document.getElementById("theLabel").innerHTML = event.data;
console.log(event.data);
$.getScript(event.data);
}
};
// send message to the page code
function button_click() {
window.parent.postMessage(document.getElementById("theMessage").value, "*");
}
</script>
</head>
<body>
<span id="theLabel" class="label"></span>
</body>
</html>
Right now, this code just prints the link in the HTML. I was doing that to verify that data was being received. It is.
What I tried previously was passing the entire js I need ( )
It seems like Wix is stripping tags when I send them - that never worked. I have also tried embedding something to try and build a ne js inside the message receive js, and that hasn’t worked.
Can anyone help me out with this?