1

I have a page where it loads an iframe (contains a form on same domain) which is hidden until the user clicks on a button, which invokes jQuery slideToggle() to show/hide it.
When a user submits the form it loads a 'thank you' page in the iframe. What then happens is that if the user then clicks on the button again, jQuery checks the ID of the iframe body element to see if the form is there, if not then it loads the form iframe and displays it.

The code I have got works in Chrome but falls over in Firefox;
Error message in console => TypeError: frames['form-iframe'] is undefined
(and I imagine IE when I get to testing it).

Any ideas on why, or how to solve this issue would be greatly appreciated.

HTML snippet

<label id="tell-a-friend-button">
    <div class="radio-button radio-button-grey">
        <div class="label-text">YES</div>   
    </div>
    <span>Tell a friend</span>
</label>

<div id="tell-a-friend">
    <iframe src="me.asp?email=foo" frameborder="0" id="form-iframe"></iframe>
</div>

jQuery snippet

$("#tell-a-friend-button").click(function(){
    // set iframe content
    $("#form-iframe").ready(function () {
        var frame = $('body', frames['form-iframe'].document).attr('id');
        if (frame == "tell-form") {
            $('#tell-a-friend').slideToggle();
        } else {
            // is the thank you page
            // load the iframe
            $("#form-iframe").attr("src", "me.asp");
        }
    });
});
Rooneyl
  • 7,802
  • 6
  • 52
  • 81
  • have you tried 'window.parent.frames' instead of frames ; also check http://stackoverflow.com/questions/1654017/how-to-expose-iframes-dom-using-jquery – Amitd Oct 02 '12 at 08:58
  • @Amitd it seems that this approach is full of potential problems, so I'm thinking of just doing it (or tying to) with ajax to get/send the form instead. Your opinion on this please. – Rooneyl Oct 02 '12 at 09:11
  • you are not doing it wrong.. i just wanted to check if 'window.parent.frames' works instead of frames['form-iframe'] .. https://developer.mozilla.org/en-US/docs/DOM/window.frames – Amitd Oct 02 '12 at 10:59
  • @Amitd decided to do it usung ajax because of the details in the link stating the problems with x-browser support. I have now done it using ajax and it seems to work fine. But thank you for your input. – Rooneyl Oct 02 '12 at 11:25

0 Answers0