
Hi,
I'm having trouble figuring out the correct way to handle the changes of context between a javascript and an HTML web resource. I have an HTML resource that includes a javascript resource. Both of the resources use Xrm and document variables. In HTML, you use parent.Xrm or parent.document to access those variable's which are outside the HTML resource, but with standalone javascript resources you use regular Xrm and document to access those variables. This causes issues when I use the included javascript functions within the HTML and independently, as the "parent or not parent" context changes. What would be the best way to approach these "context" changes? I tried having some logic that sets Xrm and document to the value from the parent context, but it didn't seem to work.
//self explanatory
Xrm = (typeof Xrm !== 'undefined') ? Xrm : parent.Xrm;
//if you can't find the element that's meant to be in the top-level document, switch to the top level context before proceeding with the rest of the code.
var newDocument = (document.getElementById("CASE")) ? document : parent.document;
tried different variations of the above, but they haven't both worked yet.
*This post is locked for comments
I have the same question (0)Hi,
the problem might be that parent.Xrm is not, yet, available, once the iframe is loaded (it may be loaded first, and, then, Xrm gets initialized in the parent)
Depending on what that web resource is supposed to do, there migth be different ways to fix it..
For example, you might try initializing that Xrm variable using setTimeout:
var InternalXrm = null;
setXrm();
function setXrm()
{
InternalXrm = (typeof Xrm !== 'undefined') ? Xrm : parent.Xrm;
if(InternalXrm == null) setTimeout(setXrm, 100);
}
And you'll need to be using InternalXrm in the script (rather than Xrm) after that..