In most scenarios, we need execution context in html webresources (for example, an IFrame embbeded on the form) is just for accessing the parent form's attributes, or do direct operation on the parent CRM form (for example, set the CRM form fields with new values, show/hide the CRM form sections or tabs).
If that is the case, the best option is using window.postMessage & event listener to communicate data between the html webresource and form JS, so you do not need to access the exuectioncontext anymore in the html webresource. You can move those logic out from html webresouces to the form JS, and process them in the receive message events.
For example:
Suppose we have a contact form, and html webresource embbeded as the contact_iframe, we have a requirement to change the contact's phone number based on the value on the iframe.
Previously it is using something like this
<html>
<script src="../clientGlobalcontext.js.aspx"></script>
<script type="text/javascript">
//Set contact form's telephone number with some value on this html
//webresource
function SetContactFormPhone(newValue){
parent.window.Xrm.Page.getAttribute('telephone1').setValue(newValue);
}
</script>
.....
</html>
Now you should Use window.postMessage in the html webresources, it will communicate between
html webresource and CRM form
<html>
<script src="../clientGlobalcontext.js.aspx"></script>
<script type="text/javascript">
//Post new telephone number to the parent contact form
function SetContactFormPhone(newValue){
var serverUrl = window.parent.Xrm.Utility.getGlobalContext().getClientUrl();
var phonenumber = {};
phonenumber.value = newValue;
window.parent.postMessage(JSON.stringify(phonenumber ), serverUrl);
}
</script>
.....
</html>
Then you can change the contact form JS,
add event listener on the parent form, and put the logic into receiveMessage callback function
//Listener is trying to get the message send from Html Webresource
function AddListener() {
if (window.XMLHttpRequest) {
//for browsers other than ie
window.parent.addEventListener("message", receiveMessage, false);
} else {
//ie
window.parent.attachEvent('onmessage', receiveMessage);
}
}
function receiveMessage(event) {
if(event.origin === serverUrl){
var phonenumber = JSON.parse(event.data)
formContext.getAttribute("telephone1").setValue(phonenumber.value)
}
}
For more details about how to using windows.postMessage and eventListener, please referencing the following article
https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage