0

How can I call a JavaScript function with parameters on it (foo('bar','yadda')) from my .page.ts file?

I have an Ionic 4 app with a page with an <iframe> on it that I need to call a JavaScript in the page displayed by the <iframe>. The .html file looks like this:

<ion-header>
</ion-header>

<ion-content>
  <iframe class='webPage' name="samplePage" [src]="cleanURL" width="100%" height="100%">
  </iframe>
</ion-content>

I intialize the the <iframe> in my .page.ts file like this:

ionViewDidEnter() {
  this.url = "https://example.com/mylegacysite"+ '?' + this.someParameter;
  this.cleanURL = this.sanitizer.bypassSecurityTrustResourceUrl(this.url);
}

It initializes fine. But later on, it would be much more efficient to be able to run the the JavaScript function on https://example.com/mylegacysite instead of updating this.cleanURL

1 Answer 1

2

With different domains, it is not possible to call methods or access the iframe's content document directly.

You need to use cross-document messaging

For example in the top window:

myIframe.contentWindow.postMessage('Hello', '*');

and in your iframe:

window.onmessage = function(e){
if (e.data == 'Hello') {
    alert('It works!');
}

};

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.