web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Xrm and document context challenge

(0) ShareShare
ReportReport
Posted on by 285

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)
  • Verified answer
    ashlega Profile Picture
    34,477 on at

    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..

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans