how to call a subroutine from an iframe - Forum

Forum Navigation
You need to log in to create posts and topics.

how to call a subroutine from an iframe

I have a subroutine ("MySub") in my VisualNEO Web app and I want to call it from within an iframe.

these commands don't work:

onclick="parent.MySub()"

onclick="window.parent.MySub()"

becaue they're trying to call a parent function, not a subroutine.

So how is it done?

thanks.

.

 

@fkapnist

If the iframe is not from the same origin as the main page, then you cannot directly access the functions or properties of the main document using parent or contentWindow due to the Same-Origin Policy, which restricts access between documents from different domains to prevent potential security attacks.

However, you can still communicate between the iframe and the main page using the postMessage() method, which is a secure way to send messages between windows (parent and iframe) from different origins.

In the iframe:
function sendMessageToParent() {
// Use postMessage to send a message to the parent (main page)
window.parent.postMessage("callMainFunction", "https://main-domain.com");
}

in your page:

// Receiving the message from the iframe
window.addEventListener("message", function(event) {
// Validate the origin of the message
if (event.origin === "https://other-domain.com") {
// If the origin is the expected one, process the message
if (event.data === "callMainFunction") {
mainFunction();
}
} else {
console.log("Invalid origin");
}
});

// Function that will be called from the iframe
function mainFunction() {
alert("Function in the main page called from the iframe!");
}

fkapnist has reacted to this post.
fkapnist

If I go to Project>Properties>Advanced>Custom Metadata

I can add a javascript function.

And then I can call it from the iframe.

So I guess my question actually is "how to call a Neoscript subroutine from a javascript function"

.

 

I think this is the way:

 

Calling actions from JavaScript

It is possible to call any action from JavaScript by just adding the "$scope." or "neo." prefix to the action name. Use the JavaScript syntax to send the parameters.

.