1

Using TypeScript I am trying to implement cross domain communication as mentioned in this thread

My code is as follows:

 var iframeElement = document.getElementById("myIframe");
 iframeElement.document.contentWindow.postMessage('hello', '*');

but in the 2nd line after document I am not getting the handle for contentwindow.

Can someone provide the solution?

Community
  • 1
  • 1
user2108168
  • 17
  • 1
  • 3
  • Welcome to SO. Please see the FAQ (http://stackoverflow.com/faq). We generally try to highlight code content so it is easier to read, and we don't usually sign our names. I'm not sure what you mean by 'handle' in your question above - can you try to explain the issue again? – Jude Fisher Feb 25 '13 at 17:28

1 Answers1

2

If what you mean by "handle" is you are not getting code completion, that is because contentWindow is a property of the iframeElement, not of the document (and also incidentally because the result of document.getElementById is a generic HtmlElement, not an iframe).

Try this:

// Cast the result of getElementById, so you get an iframe and not a generic HtmlElement:
var iframeElement:HTMLIFrameElement = <HTMLIFrameElement>document.getElementById("myIframe");
// Then reference the contentWindow property of the iframe element
iframeElement.contentWindow.postMessage('hello', '*');
Jude Fisher
  • 10,688
  • 6
  • 44
  • 86