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

Announcements

No record found.

News and Announcements icon
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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
JS-09031509-0 Profile Picture

JS-09031509-0 3

#2
AS-17030037-0 Profile Picture

AS-17030037-0 2

#2
Mark Eckert Profile Picture

Mark Eckert 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans