title: JQuery IFrame Load event firing too soon
body: <p>I have tried in vain to find a solution for this issue, but to no avail. Basically I am trying use JQuery to create a new dialog when a button on the page is clicked. The content of the dialog contains an IFrame referencing external (cross-domain) content. Inside the dialog I also want a "back" link (will change to a button later on) that acts similar to the browsers back button. Granted I could just get the user to use the normal back button, but this isn't really all that intuitive hence I want the back button simply inside the dialog itself above the iframe. </p>
  
  <p>The javascript for this is relatively simple using the "history" object. However, I want the ability to disable / remove this "back" link when the history count exceeds a certain level. The details of the disabling functionality are trivial enough, however I need to be able to access the load event of the iframe to determine when the content has finished loading so that the history.length value will be correct for the purposes of my test. However the load event seems to be fired too soon and the history value is not correct at the time the load event is fired. The history value appears to be correct when going "forwards" but after clicking my impromtu back button, the history value is no longer correct as it appears the load event is reporting a history value <em>prior</em> to the iframe having loaded the content. </p>
  
  <p>Here is my current javascript:</p>
  
  <pre><code>&lt;script language="javascript" type="text/javascript"&gt;
  var baseHistory = 0;
  
  $(function() {
    $("body").append('&lt;div id="RedmapDialog" title="Dialog Title"&gt;&lt;/div&gt;');
    $(".RedmapButton").bind('click', function(event) {
      baseHistory = history.length;
      var data='&lt;a href = "javascript:history.go(-1)"&gt;Back to previous page&lt;/a&gt;'
      data = data + '&lt;iframe width="100%" height="100%" id="RMFrame"&gt;&lt;/iframe&gt;'
      $("#RedmapDialog").html(data);
      $("#RedmapDialog").dialog("option", "title", 'Document: ');
      $("#RedmapDialog").dialog("open");
      $('#RMFrame').load(function() {
        alert("history.length=" + history.length);
      }).attr("src", "http://www.google.com/");
    });
  
    $("#RedmapDialog").dialog({  
      closeOnEscape: false,    
      height: 600,
      width: 800,
      autoOpen: false,    
      resizable: true,
      autoResize:true,
      modal: false           
    });  
  });
  
  &lt;/script&gt;
  </code></pre>
  
  <p>and here is the calling html:</p>
  
  <pre><code>&lt;div class="RedmapButton" style="cursor: pointer; cursor: hand;"&gt;
    &lt;img src="/gui/images/bullseye.gif" width=26 height=26 alt="Redmap" title="Redmap"&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Any thought's as to why the load event is being called so soon after I select my back link? I understand that there are some cross-domain / same origin policy issues at play here, however I am not interested in the actual content of the iframe, simply the history.length value only. </p>
  
  <p>Edit: Updated this post to include a suggestion by ShankarSangoli for setting the src of my iframe after its instantiation. However, I am still encountering the original issue whereby my "back" link still reports an incorrect history.length. Here is a jsfiddle link <a href="http://jsfiddle.net/fEgv9/1" rel="nofollow">http://jsfiddle.net/fEgv9/1</a> that illustrates my issue. When run, click the "Redmap" image to display the iframe. Then click any link from google, and then hit my "Back to previous page" link, note that the history is not reporting correctly.</p>
  
  <p>Thanks in advance.</p>