[英]How do I call a JavaScript function in the webpage displayed by an iframe, from a TypeScript file in Ionic 4?
How can I call a JavaScript function with parameters on it ( foo('bar','yadda')
) from my .page.ts
file?如何从我的
.page.ts
文件中调用带有参数的 JavaScript function ( foo('bar','yadda')
)?
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>
.我有一个带有
<iframe>
页面的 Ionic 4 应用程序,我需要在<iframe>
显示的页面中调用 JavaScript 。 The .html
file looks like this: .html
文件如下所示:
<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:我像这样初始化 my.page.ts 文件中的
<iframe>
:
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
但是稍后,能够在https://example.com/mylegacysite上运行 JavaScript
this.cleanURL
会更有效
With different domains, it is not possible to call methods or access the iframe's content document directly.对于不同的域,无法直接调用方法或访问 iframe 的内容文档。
You need to use cross-document messaging您需要使用跨文档消息传递
For example in the top window:例如在顶部 window 中:
myIframe.contentWindow.postMessage('Hello', '*');
and in your iframe:在您的 iframe 中:
window.onmessage = function(e){
if (e.data == 'Hello') {
alert('It works!');
}
}; };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.