6

I have an iframe loading into a parent page. The iframe contains a sequence of forms, and I'd like to take action on the parent page every time the iframe content in reloaded (ie, after a form is submitted in the iframe content). The current code is located on the parent page and works in all the big players except IE (I'm only concerned with IE 7 & 8).

var iframe = document.getElementById('theiframe');
function refresh( ) {
  alert('foo');
}
if (iframe.attachEvent) iframe.attachEvent('onload', refresh);
else iframe.onload = refresh;

What am I missing that would prevent this from being effective in IE?

Chris Tonkinson
  • 13,823
  • 14
  • 58
  • 90
  • Try referencing the iframe's document: var iframe = document.getElementById('theiframe').document; (not an answer because I'm guessing :) ) – Cfreak Jun 23 '11 at 14:38

2 Answers2

4

Replace:

iframe.onload = refresh

with:

iframe.attachEvent('load', refresh, false); 


To clear any confusion:
var iframe = document.getElementById('theiframe');
function refresh( ) {
  alert('foo');
}

if (iframe.attachEvent) iframe.attachEvent('onload', refresh);
else iframe.addEventListener('load', refresh, false)
cypher
  • 429
  • 1
  • 4
  • 16
Joe
  • 80,724
  • 18
  • 127
  • 145
1

You can call it from the iframe by calling parent.refresh() when you are done with a submit.

Mic
  • 24,812
  • 9
  • 57
  • 70
  • While I ultimately have control over both the parent and iframe, modifying the iframe in any way is prohibitively difficult. – Chris Tonkinson Jun 23 '11 at 15:40
  • 1
    ok, but sometimes the `onload` event fires, while all the job is not done in the `iframe`. I found adding the "I'm done" command in the iframe more reliable for all browsers. – Mic Jun 23 '11 at 16:27
  • In this case it doesn't matter that the iframe contents (resources, DOM, etc) are or aren't finished loading, it just matters that "the user has begun viewing a new page." Great point though. – Chris Tonkinson Jun 23 '11 at 18:20
  • This solution did not work for me in IE 11. The parent page remained unaffected. – Mark Dec 13 '16 at 19:25
  • @Mic, my motivation is to provide information to people who are arriving here today from google searches. They will likely be looking for a solution to a current problem; not a problem from 5 years ago. To reiterate, not a personal attack, merely saving some people some time. – Mark Dec 17 '16 at 20:07
  • @Mark, if IE does not accept that anymore, you can use `parent.postMessage` with a listener in the parent's page that will refresh itself – Mic Dec 19 '16 at 08:42