1

I've created an iFrame dynamically with Mootools. In my iFrame, I want to execute a function to automatically populate it. So far, I've the following code:

    var mApp = this.app;
    var iframe = new Element('iframe', {
      'src' : 'frame_fittingbox.html',
      'style' : 'height:100%;width:100%;background:transparent;border:none;',
      'events' : {
        'load' : function() {
          var doc_iframe = (iframe.contentWindow) ? iframe.contentWindow : (iframe.contentDocument.document) ? iframe.contentDocument.document : iframe.contentDocument;
          doc_iframe.run(mApp, 'demo'); // FittingBox account
        },
      },
    });

    var container = this.app.page();
    iframe.inject(container);

In the iFrame, I've that code:

<html>
  <head>
    <script src="./app/components/exported_jsfile.js" />
    <script>
      function run(app, account) {
        document.write('\x3Cscript type="text/javascript" src="http://my.website.com/js/' +account+ '/jsfile.js">\x3C/script>');

        var customClass = new My_Custom_Class(app, document.body);
        customClass .open();
      }
    </script>
  </head>

  <body>
    Loading ...
  </body>
</html>

It doesn't work as the run function is never called. In the console, I got the following message: Uncaught TypeError: Object [object global] has no method 'run'. What did I make wrong ?

Thanks for your precious help.

5
  • can you console.log(doc_iframe) in the load event handler?
    – wroniasty
    Feb 5, 2014 at 9:22
  • You cannot use document.write after page load
    – mplungjan
    Feb 5, 2014 at 9:24
  • I've logged doc_iframe but it gives me too many attributes to list them all.
    – Manitoba
    Feb 5, 2014 at 9:27
  • Looks good to me. Are you sure your run() function is not inside another function? like onload()? Check here: jsfiddle.net/ukKT6
    – Sergio
    Feb 8, 2014 at 11:26
  • Nope it doesn't work. I've made it working in a reversed way. I mean, the iframe is triggering parent window functions.
    – Manitoba
    Feb 17, 2014 at 11:40

1 Answer 1

1

The solution is to use a callback in the parent frame. So, when you need to perfom an action, use the following code:

parent.window.parent.actionFromParent();

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.