jQuery .ready in a dynamically inserted iframe

后端 未结 10 2037
闹比i
闹比i 2020-11-22 07:11

We are using jQuery thickbox to dynamically display an iframe when someone clicks on a picture. In this iframe, we are using galleria a javascript library to display multip

10条回答
  •  醉酒成梦
    2020-11-22 07:49

    Using jQuery 1.3.2 the following worked for me:

    $('iframe').ready(function() {
      $('body', $('iframe').contents()).html('Hello World!');
    });
    

    REVISION:! Actually the above code sometimes looks like it works in Firefox, never looks like it works in Opera.

    Instead I implemented a polling solution for my purposes. Simplified down it looks like this:

    $(function() {
      function manipIframe() {
        el = $('body', $('iframe').contents());
        if (el.length != 1) {
          setTimeout(manipIframe, 100);
          return;
        }
        el.html('Hello World!');
      }
      manipIframe();
    });
    

    This doesn't require code in the called iframe pages. All code resides and executes from the parent frame/window.

提交回复
热议问题