views:

501

answers:

3

I have iframe with with form elements such as input, select textarea etc. I need to get whole iframe content with entered values to those elements. In IE 7,8 it work fine, but in FF I'm getting empty or default values instead.

Here is code snippets:

Iframe content

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
  </head>
  <body>
    <div id="content">
      <input name="fn" type="text" id="fn"/>
      <select name="states">
        <option val=""></option>
        <option val="ca">CA</option>
        <option val="co">CO</option>
        <option val="ce">CE</option>
        <option val="cI">CI</option>
      </select>
    </div>
  </body>
</html>

Javascript inside the parent page

  function getContentFromIframe(iFrameName){  
      var myIFrame = document.getElementById(iFrameName);
      var content = myIFrame.contentWindow.document.body.innerHTML;
  }
  //calling function for example on button click
  alert(getContentFromIframe('frame1'));

I entered value in input and chose something in select and ran script above. In IE I'm getting content with entered values, but ff and other browsers returns only html as it was loaded, although I can retrieve values separately for example with jquery

  $('#frame1').contents().find('#fn').val()

I really stack with this problem. Please help me. Thanks.

May be there is any other way to get this content without using innerHTML?

+1  A: 

This has nothing to do with being in a frame -- innerHTML behaves differently on the different browsers. Even the simple case

<div id=mydiv><input name=in id=inval value=100></div>

will exhibit the same behavior. I don't see an alternative to retrieving the values separately.

Lucky
Thanks for your answer. Yes I agree, simple innerHTML in page doing the same unfortunately...
Is there any other way to get whole content without innerHTML?
A: 

If you are using jquery, you can retrieve all inputs' name/value pairs by using the serialize() method. Just wrap your input fields into a form element and than something like this to retrieve them:

var input_vals=$("iframe").contents().find("form").serialize();

//output:
//"fn=bla&states=CO"
And
A: 

Actually solution was posted on StackOverflow but I miss the original. May be someone else will need this feature I'm reposting code here with my little updates

This jquery plugin just updates fields before we get innerHtml

(function($) {
    var oldHTML = $.fn.html;

    $.fn.formhtml = function() {
        if (arguments.length) return oldHTML.apply(this, arguments);
        $("input,textarea,button", this).each(function() {
            this.setAttribute('value', this.value);
        });

        $("select option", this).each(function() {
            if (this.selected)
                this.setAttribute('selected', 'selected');
            else
                this.removeAttribute('selected');
        });

        $("textarea", this).each(function() {
            $(this).html(this.value);
        });

        $(":radio,:checkbox", this).each(function() {
            // im not really even sure you need to do this for "checked"
            // but what the heck, better safe than sorry
            if (this.checked) this.setAttribute('checked', 'checked');
            else this.removeAttribute('checked');
        });
        return oldHTML.apply(this);
    };

    //optional to override real .html() if you want
    // $.fn.html = $.fn.formhtml;
})(jQuery);