无法使用jQuery写入动态iframe [英] Can't write to dynamic iframe using jQuery

查看:103
本文介绍了无法使用jQuery写入动态iframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是动态创建iframe,然后使用jQuery(例如Google AdSense脚本)将广告JavaScript写入其中.我的代码可在Chrome上运行,但在Firefox中会间歇性地失败,即有时广告脚本会运行并呈现广告,而其他时候则不会.当它不起作用时,脚本代码本身会显示在iframe中.

My goal is to dynamically create an iframe and write ad JavaScript into it using jQuery (e.g. Google AdSense script). My code works on Chrome, but fails intermittently in Firefox i.e. sometimes the ad script runs and renders the ad, and other times it doesn't. When it doesn't work, the script code itself shows up in the iframe.

我的猜测是发生这些间歇性故障,因为在我写iframe时iframe还没有准备好.我尝试了 iframe_html 的各种迭代(我的函数名,应该等待iframe准备就绪),但是没有运气.任何帮助表示赞赏!

My guess is these intermittent failures occur because the iframe is not ready by the time I write to it. I have tried various iterations of iframe_html (my name for the function which is supposed to wait for the iframe to be ready), but no luck. Any help appreciated!

PS:我已经阅读了各种主题(例如 jQuery .ready in a dynamic插入的iframe ).只是让所有人都知道我已经对此进行了研究,但是我被困住了:)

PS: I have read various threads (e.g. jQuery .ready in a dynamically inserted iframe). Just letting everyone know that I've done my research on this, but I'm stuck :)

迭代1:

function iframe_html(html){
 $('<iframe name ="myiframe" id="myiframe"/>').appendTo('#maindiv');
 $('#myiframe').load(
   function(){
    $('#myiframe').ready( function(){
     var d = $("#myiframe")[0].contentWindow.document;
     d.open();
     d.close();
     d.write(html);
     });
   }
 );
};

迭代2:

function iframe_html(html){
 $('<iframe id="myiframe"/>').appendTo('#maindiv').ready(
   function(){
    $("#myiframe").contents().get(0).write(html);
   }
 ); 
};

推荐答案

老实说,在处理iframe上的加载事件时,我发现的最简单,最可靠的方法是在实际iframe标记中使用"onload"属性.一旦"onload"事件触发,设置内容就不会有太多问题.这是一个示例:

Honestly, the easiest and most reliable way I have found when dealing with the load events on iframes uses the "onload" attribute in the actual iframe tag. I have never had much of a problem with setting the content once the "onload" event fires. Here is an example:

<html>
    <head>
        <script type='text/javascript' src='jquery-1.3.2.js'></script>
        <script type='text/javascript'>
            $(function() {
                var $iframe = $("<iframe id='myiframe' name='myiframe' src='iframe.html' onload='iframe_load()'></iframe>");
                $("body").append($iframe);
            });
            function iframe_load() {
                var doc = $("#myiframe").contents()[0];
                $(doc.body).html("hi");
            }
        </script>
    </head>
    <body></body>
</html>

此问题是您必须使用属性标签和全局函数声明.如果您绝对不能拥有这些东西之一,那么我就不会对此有任何疑问(尽管它看起来与您的尝试并没有太大不同,所以我不确定):

The problem with this is that you have to use attribute tags and global function declarations. If you absolutely CAN'T have one of these things, I haven't had problems with this (although it doesn't look much different than your attempts, so I'm not sure):

<html>
    <head>
        <script type='text/javascript' src='jquery-1.3.2.js'></script>
        <script type='text/javascript'>
            $(function() {
                var $iframe = $("<iframe id='myiframe' name='myiframe' src='iframe.html'></iframe>");
                $iframe.load(iframe_load);
                $("body").append($iframe);
            });
            function iframe_load() {
                var doc = $("#myiframe").contents()[0];
                $(doc.body).html("hi");
            }
        </script>
    </head>
    <body></body>
</html>

这是DOM和JavaScript中最令人沮丧的部分之一-我对您表示哀悼.如果这些都不起作用,请打开Firebug并告诉我错误消息是什么.

This is one of the most frustrating parts of the DOM and JavaScript - my condolences are with you. If neither of these work, then open up Firebug and tell me what the error message is.

这篇关于无法使用jQuery写入动态iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆