سؤال

I need help with the window.postMessage method. I'm trying to post a message to an iframe from the parent window.

The iframe's name and ID is myFrame, and I have tried the following code, with no avail:

document.getElementById('myFrame').contentWindow.postMessage('Hello','*')
هل كانت مفيدة؟

المحلول

Take a step back and debug.

  1. What does document.getElementById('myIframe') give you?
  2. What does document.getElementById('myIframe').contentWindow give you?
  3. Do you have a global method named postMessage in the iframe?
  4. Is the iframe domain the same as the initial page? It has to be.
  5. Does the browser throw any errors in the console?
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top