Skip to content

Instantly share code, notes, and snippets.

@bizfreak22
Forked from pbojinov/README.md
Created July 7, 2020 17:04
Show Gist options
  • Save bizfreak22/b1cb5789b531d5bdc8b7625d8ec70e2f to your computer and use it in GitHub Desktop.
Save bizfreak22/b1cb5789b531d5bdc8b7625d8ec70e2f to your computer and use it in GitHub Desktop.

Revisions

  1. @pbojinov pbojinov revised this gist Mar 15, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -12,6 +12,6 @@ Recieve messages using `window.addEventListener('message')`
    Send messages to parent window using `window.parent.postMessage`
    Recieve messages using `window.addEventListener('message')`

    ## Working Example:
    ## Live Example

    [http://pbojinov.github.io/iframe-communication/](http://pbojinov.github.io/iframe-communication/)
  2. @pbojinov pbojinov revised this gist Mar 15, 2017. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -11,3 +11,7 @@ Recieve messages using `window.addEventListener('message')`

    Send messages to parent window using `window.parent.postMessage`
    Recieve messages using `window.addEventListener('message')`

    ## Working Example:

    [http://pbojinov.github.io/iframe-communication/](http://pbojinov.github.io/iframe-communication/)
  3. @pbojinov pbojinov revised this gist Feb 12, 2014. 2 changed files with 0 additions and 2 deletions.
    1 change: 0 additions & 1 deletion iframe.html
    Original file line number Diff line number Diff line change
    @@ -47,6 +47,5 @@ <h1>Hello there, i'm an iframe</h1>
    sendMessage('' + random);
    });
    </script>
    </div>
    </body>
    </html>
    1 change: 0 additions & 1 deletion parent.html
    Original file line number Diff line number Diff line change
    @@ -55,6 +55,5 @@ <h1>Parent Window</h1>
    results.innerHTML = e.data;
    });
    </script>
    </div>
    </body>
    </html>
  4. @pbojinov pbojinov revised this gist Feb 12, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # Two way iframe communication

    The only thing different between the two pages is the method of sending message. Recieving messages is the same in both.
    The main difference between the two pages is the method of sending messages. Recieving messages is the same in both.

    ## Parent

  5. @pbojinov pbojinov revised this gist Feb 12, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -9,5 +9,5 @@ Recieve messages using `window.addEventListener('message')`

    ## iframe

    Send messages to parent window using `window.parent.postMessag`
    Send messages to parent window using `window.parent.postMessage`
    Recieve messages using `window.addEventListener('message')`
  6. @pbojinov pbojinov revised this gist Feb 12, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion parent.html
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@ <h1>Parent Window</h1>
    }
    }

    var iframeSource = 'https://gist.github.com/pbojinov/8965011/raw/3c9969996662b6b30d70f8e0bf7d3a7c077f385b/iframe.html';
    var iframeSource = 'https://gist.github.com/pbojinov/8965299/raw/fadf2c4058b6481646e7244994c1890f2ad81b60/iframe.html';

    // Create the iframe
    var iframe = document.createElement('iframe');
  7. @pbojinov pbojinov created this gist Feb 12, 2014.
    13 changes: 13 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    # Two way iframe communication

    The only thing different between the two pages is the method of sending message. Recieving messages is the same in both.

    ## Parent

    Send messages to iframe using `iframeEl.contentWindow.postMessage`
    Recieve messages using `window.addEventListener('message')`

    ## iframe

    Send messages to parent window using `window.parent.postMessag`
    Recieve messages using `window.addEventListener('message')`
    52 changes: 52 additions & 0 deletions iframe.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    <!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>
    </div>
    </body>
    </html>
    60 changes: 60 additions & 0 deletions parent.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <title>Parent Window</title>
    </head>
    <body>

    <h1>Parent Window</h1>
    <p>Send Message: <button id="message_button">Hi there iframe</button></p>
    <p>Got Message:</p>
    <div id="results"></div>
    <br/>

    <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);
    }
    }

    var iframeSource = 'https://gist.github.com/pbojinov/8965011/raw/3c9969996662b6b30d70f8e0bf7d3a7c077f385b/iframe.html';

    // Create the iframe
    var iframe = document.createElement('iframe');
    iframe.setAttribute('src', iframeSource);
    iframe.setAttribute('id', 'the_iframe');
    iframe.style.width = 450 + 'px';
    iframe.style.height = 200 + 'px';
    document.body.appendChild(iframe);

    // Send a message to the child iframe
    var iframeEl = document.getElementById('the_iframe'),
    messageButton = document.getElementById('message_button'),
    results = document.getElementById('results');


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

    // Send random messge data on every button click
    bindEvent(messageButton, 'click', function (e) {
    var random = Math.random();
    sendMessage('' + random);
    });

    // Listen to message from child window
    bindEvent(window, 'message', function (e) {
    results.innerHTML = e.data;
    });
    </script>
    </div>
    </body>
    </html>