0

I'm using iframe in ionic 3 to pass data between parent and child.

on clicking on the button I'm able to pass the data using iframe.contentWindow.postMessage('value', '*')

but on every time on button click, iframe getting refreshed.

I do the following:

Ionic 3 html code /home.html

<button ion-button id="message_button" (click)="sendMessage()">My iframe</button>

<iframe id="the_iframe" #iframe height="100%" width="100%" [src]="urlpaste()" name="iframe_a"></iframe>

Ionic 3 typescript code /home.ts

import { Component, ViewChild, ElementRef } from '@angular/core';
import { NavController } from 'ionic-angular';
import { DomSanitizer} from '@angular/platform-browser';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})

export class HomePage {

  @ViewChild('iframe') iframe: ElementRef;
  myIframe:any;

  constructor(public navCtrl: NavController, private sanitizer: DomSanitizer) {}

  urlpaste(){
    this.url = "http://localhost:5000/iframe.html";
    return this.sanitizer.bypassSecurityTrustResourceUrl(this.url);
  }

  ngAfterViewInit() {
    this.myIframe = this.iframe.nativeElement.contentWindow
  }

  sendMessage() {
    var random = Math.random();
    this.myIframe.postMessage(random, '*');
  }

}

Plain HTML code /iframe.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>iframe Window</title>
    <style>
        body {
            background-color: #D53C2F;
            color: white;
        }
    </style>
</head>
<body>

    <h1>Hello there, i'm an iframe</h1>
    <p>Send Message: <button id="message_button">Hi parent</button></p>
    <p>Got Message:</p>
    <div id="results"></div>

    <script>
        // addEventListener support for IE8
        function bindEvent(element, eventName, eventHandler) {
            if (element.addEventListener) {
                element.addEventListener(eventName, eventHandler, false);
            } else if (element.attachEvent) {
                element.attachEvent('on' + eventName, eventHandler);
            }
        }

        // Send a message to the parent
        var sendMessage = function (msg) {
            // Make sure you are sending a string, and to stringify JSON
            window.parent.postMessage(msg, '*');
        };

        var results = document.getElementById('results'),
            messageButton = document.getElementById('message_button');

        // Listen to messages from parent window
        bindEvent(window, 'message', function (e) {
            results.innerHTML = e.data;
        });

        // Send random message data on every button click
        bindEvent(messageButton, 'click', function (e) {
            var random = Math.random();
            sendMessage('' + random);
        });
    </script>
</body>
</html>

1 Answer 1

1

I know it little bit late. But it will definitely help someone in future.

HTML

<iframe id="the_iframe" #iframe height="100%" width="100%" [src]="url" name="iframe_a"></iframe>

TS

    url = "";
    constructor(public navCtrl: NavController, private sanitizer: DomSanitizer) {
     this.url = this.urlpaste();
    }

  urlpaste(){
    let url = "http://localhost:5000/iframe.html";
    return this.sanitizer.bypassSecurityTrustResourceUrl(url);
  }

You have to use "url" variable in iframe src. Don't function directly there...

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.