views:

115

answers:

1
+1  Q: 

iFrame Validation

Hi,

I currently have an iframe within my main page that has a number of checkboxes that need to be actioned prior to leaving the iframe, i.e, if the user commences checking the checkboxes and half way through they then click back on the main page, i.e leaving the iframe, I would like to be able to trap/validate that they have left the iframe and prompt them with a message indicating this, say with a "Note: You will lose all data entered here - Leave: Yes/No?" type message.

Any help/websites would be appreciated.

Thanks. Tony.

+1  A: 

Prompting a User to Save When Leaving a Page. This 4guys article sounds like what you need. It talks about the onbeforeunload event. There's some awesome posts here on stackoverflow about onbeforeunload too.


It appears that onbeforeunload indeed does not fire for an iframe. Bugger!

Here's some sample code that should work though. This will only work if you're in the same domain, otherwise same origin policy will prevent the iframe from talking back to the parent.

I also haven't tested these in many browsers so YMMV.

You've got two options here, depending on where you want to put the prompt for changes logic.

Option one involves the iframe telling the parent window when there's changes.

Parent window javascript:

    window.onbeforeunload=closeIt;
    var changes = false;
    function closeIt()
    {
      if (changes)
      {
          return "Yo, changes, save 'em?";
      }
    }
    function somethingChanged() {
        changes=true;
    };

Iframe javascript:

    $(function() {
        $('input').change(parent.somethingChanged);
    });

Option two involves the iframe taking control over the parent window's onbeforeunload

Parent window javascript:

There is none :-)

Iframe javascript:

    $(function() {
        parent.window.onbeforeunload = myCloseIt;
        $('input').change(somethingChanged);
    });
    var changes = false;
    function myCloseIt()
    {
      if (changes)
      {
          return "Yo, changes, save 'em?";
      }
    }
    function somethingChanged() {
        changes=true;
    };

In either option the naive changes variable could be beefed up a bit, probably using techniques from the 4guys article, to see if there's really been any changes.


If they're on different domains, but you're still in charge of "both sides" of the HTML, there's still some options, they're just harder.

xssinterface is a library that uses postMessage and location hashes and secret voodoo black magic to communicate cross site.

Dan F
Thanks Dan - just hoping this works with iframes?
tonsils
It might need a few mods (like reaching into the iframe to get the checkboxes), but the concept still holds. Holler if you have issues and I'll see if I can work up a sample
Dan F
Hi Dan, unfortunately I can't seem to get it to fire at the iframe level, only at the parent. Would appreciate if you could point me in the right direction with some sample code in order to trap at iframe level - thanks.
tonsils
No worries, I'll try some ideas in the morning and update the answer. It's half past 1am here in aus, I'm off to bed :-)
Dan F
Thanks for that Dan, really appreciate it. I will have a play and get back to you on any queries, if that's ok?
tonsils
Yep, no worries, fire away if you've got dramas
Dan F