1

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.

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: 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 :)

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

Iteration 2:

function iframe_html(html){
 $('<iframe id="myiframe"/>').appendTo('#maindiv').ready(
   function(){
    $("#myiframe").contents().get(0).write(html);
   }
 ); 
};
2
  • try setting src on the iframe to about:blank? Commented Nov 2, 2009 at 23:58
  • did you ever solve this issue? Commented Nov 11, 2010 at 0:33

2 Answers 2

3

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>

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Cool Solution!! +1, Can you add a function that will detect when dynamic data completes loading in iframe after $(doc.body).html("hi!") ?
-1

false.html:

<html>
<head><title></title></head>
<body></body>
</html>

JS:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript">

function iframe_html(html)
{
 var id = "myiframe_" + ((new Date()).getTime());

 $('<iframe src="false.html" name ="'+id+'" id="'+id+'" />').appendTo('#maindiv');

 var loadIFrame = function()
 {
      var elIF = window.document.frames[id];
      if (elIF.window.document.readyState!="complete") 
      {
        setTimeout(loadIFrame, 100);
        return false;
      }

      $(elIF.window.document).find("body").html(html);
 }

  loadIFrame();


};
$(function(){

    iframe_html("<div>hola</div>");

}); 
</script>
</head>
<body>
<div id="maindiv"></div>
</body>
</html>

then please see this link

2 Comments

That did not work. I have the same problem as before, which is that the ad script does not execute intermittently.
I modify the example, look after the link to see the compatibility of window.document.frames[x].

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.