刚刚附加后访问iframe

时间:2017-12-04 07:35:05

标签: javascript jquery

我的版本不起作用:

 $(window).on('load', function() {
      var $iframe = $("<iframe>", 
                 {id:"up2europe",
                 marginheight:"5",
                 marginwidth:"5",
                 src:"https://www.up2europe.eu/widget/go/181d4bdc08289da3d78b79ee5f9e7e2d"
                 }); 

       $(".sidebar__right:first").append($iframe).on('load', function() 
             alert(document.getElementbyId("#up2europe").documentElement.title);                                                                                                
       });
});

所以...我正在尝试访问iframe的标题...以检查它是否正确加载。

2 个答案:

答案 0 :(得分:0)

您的代码还有一些问题:

  1. 您使用$("...").append($iframe),在附加$("...")后返回iframe。这意味着您的load事件附加到$("...")而不是iframe,这在这种情况下是合乎逻辑的事情。要实现该使用$iframe.appendTo("..."),以便方法的返回值为$iframe

  2. 您在上述getElementbyId事件监听器中使用load,这基本上是写getElementById的错误方式。

  3. 您在事件监听器中传递给alert的参数并不是获取title文档iframe的有效方法。

  4. 因此,通过纠正上述错误,代码变为:

    $(window).on('load', function() {
      /* Create the iframe. */
      var $iframe = $("<iframe>", {
        id: "up2europe",
        marginheight: "5",
        marginwidth: "5",
        src: "https://www.up2europe.eu/widget/go/181d4bdc08289da3d78b79ee5f9e7e2d"
      });
    
      /* Using 'appendTo' ensures we add the 'load' event listener to the iframe. */
      $iframe.appendTo(".sidebar__right:first").on('load', function() {
        /* 'this' refers to the iframe. */
        alert(this.contentDocument.title);
      });
    

    注意:为了实际获取title文档的iframe,必须同时提供iframe和父窗口的内容在同一个域中,否则您的代码将因Same-Origin Policy而失败。

    <强>段:
    (由于同源策略导致文档标题失败;请读取抛出的错误。)

    &#13;
    &#13;
    $(window).on('load', function() {
      /* Create the iframe. */
      var $iframe = $("<iframe>", {
        id: "up2europe",
        marginheight: "5",
        marginwidth: "5",
        src: "https://www.up2europe.eu/widget/go/181d4bdc08289da3d78b79ee5f9e7e2d"
      });
    
      /* Using 'appendTo' ensures we add the 'load' event listener to the iframe. */
      $iframe.appendTo(".sidebar__right:first").on('load', function() {
        /* 'this' refers to the iframe. */
        alert(this.contentDocument.title);
      });
    });
    &#13;
    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class = "sidebar__right"></div>
    &#13;
    &#13;
    &#13;

    资源:您可以详细了解Same-Origin Policy here

    如果您只想查看iframe的内容是否已加载,则无需查看其文档title,而只需附加load 1}} event`:

    $iframe.on("load", function () {
       // code to execute after it's loaded.
    });
    

答案 1 :(得分:-1)

我相信这是使用jQuery添加元素的正确方法。退房LINK

<强>代码:

 $(window).on('load', function() {
   var iframe = document.createElement('iframe');
   $(iframe).css({'marginheight':'5px','marginwidth':'5px'});
   $(iframe).attr({'src':'https://www.up2europe.eu/widget/go/181d4bdc08289da3d78b79ee5f9e7e2d','id':'up2europe','title':'Im iframe'});
   $(".sidebar__right").append(iframe);

    $(iframe).ready(function(){
       alert($(iframe).attr('title'));
    });

 });