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)

Sending variable from IFrame to CRM Form using postmessage

(0) ShareShare
ReportReport
Posted on by

Hi all,

I have a problem with my code - onload of my CRM 2016 OnPremise form I add an eventlistener. On my iframe, I am using window.postmessage, hoping that my listener will then handle the returned variable. However when I debug, the listener seems to set up but when I click the button on the iframe to invoke the postmessage, the code within the listener event does not seem to invoke at all. Can anyone spot anything obvious? Code is below:

IFRAME javascript:

var score = document.getElementById("elementname").innerText; //gets the score ok

window.parent.postMessage(score, window.location.protocol + "//" + window.location.host);

CRM FORM ONLOAD:

function loadListener()
{
if (window.addEventListener)
{
addEventListener('message', listener, false)
}
else
{
attachEvent('onmessage', listener)
}
}

function listener(event)
{
if (event.origin == (window.location.protocol + "//" + window.location.host))
{
alert("hello"); //never runs
alert(event.data);


}
}

*This post is locked for comments

I have the same question (0)
  • ScottDurow Profile Picture
    21 on at

    Is there a reason why you simply can't invoke a method directly on the parent window?

  • Community Member Profile Picture
    on at

    Hi Scott,

    Thanks for your reply. I haven't tried to do it that way, but I did try and obtain the variable from the iframe using Xrm's getObject and then drilling down into the document, but I got an access denied because the sites are on different domains (despite disabling the checkbox which prevents crosssite scripting)

    I was able to get my code working, but to do this I had to use the wildcard as follows:

    window.parent.postMessage(score, "*");

    and in the listener event I now have

    if (event.origin == "fixedurlwhereiframeishosted")

    I understand there are security issues with the wildcard? If so, why would the previous code not work but this did? Thanks

  • Community Member Profile Picture
    on at

    I am having the exact same problem except that using the wildcard still didnt work me.  Is there anything else you did to make this work?

    The same code worked in CRM 2015 but now it suddenly stopped working.  I did notice that the URLReferrer from within the iframe is vastly different then what it used to be in CRM 2015.  That's what I used to use as the target URL.  Either way the wildcard "*" should of worked but it isnt so any help would be appreciated.

  • Piotr Gaszewski Profile Picture
    424 on at

    The same problem. Any new ideas :) ?

  • Suggested answer
    Sabina Khanna Profile Picture
    15 on at

    First of all you are writing the javascript on 'onLoad' event that means you are executing above CRM listener before iFrame is displayed. If you execute like above then you will get an error saying "event.origin is not defined".

    Second thing is you need to write the eventListener after the iFrame's ready state Completes so that it ca listen the data and make sure the hierarchy structure shouldn't break because if you are writing your script on iFrame's event 'setReadyStateComplete' then you have to use "window.parent.addEventListener( ... )" because now hierarchy has been changed as you are now on one level below the window.

    For any doubt please feel free to ask again.

    Thanks!

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