14

Ok, I am going to rephrase my request as I think based on some of the answers, it got convoluted. All I am looking for is if there is a javascript command to fire the onload event from the javascript in the parent. A line of code such as: document.getElementById('FrameID').fire.onload(); or if this cannot be done.

I am brainstorming an application where I am going to preload some iframes with url's, say 10 of them. I am then going to rotate them by hiding and displaying the frames. I want to be able to fire a window onload event after the active frame is displayed without reloading the page so the page will act as if it is fresh if it has an onload event. Can I do this? The pages may or may not have a window onload event.

5
  • fire a window onload event where? in the parent window? Or in a given iframe's window?
    – Madbreaks
    Commented Mar 10, 2012 at 0:45
  • The javascript would be in the parent which is running the show.
    – user999684
    Commented Mar 10, 2012 at 0:47
  • One more question - plain ol' JavaScript, or jQuery, etc.?
    – Madbreaks
    Commented Mar 10, 2012 at 0:49
  • stackoverflow.com/questions/1654017/…
    – Matt Esch
    Commented Mar 10, 2012 at 0:54
  • Either, but plain ol javascript would be preferred. I have not written any code yet, just trying to get through this hurdle in my methodology.
    – user999684
    Commented Mar 10, 2012 at 1:03

5 Answers 5

38

This is the current method for re-emitting the load event:

dispatchEvent(new Event('load'));

https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events

Browser compatibility:

  • Chrome: 15+
  • Firefox: 11+
  • Microsoft Edge: all versions
  • Opera: 11.60+
  • Safari: 6+
  • Internet Explorer: none (see Dave's answer)
2
  • 5
    Doesn't work on that, uh, "browser" named Internet Explorer.
    – alecov
    Commented May 31, 2016 at 18:25
  • 2
    Downvoted because this doesn't work in IE -- see text in the link: "This constructor is supported in most modern browsers (with Internet Explorer being the exception). For a more verbose approach (which works with Internet Explorer), see the old-fashioned way below."
    – bonez
    Commented Jun 6, 2018 at 19:50
14

You need to re-emit the load event:

var evt = document.createEvent('Event');  
evt.initEvent('load', false, false);  
window.dispatchEvent(evt);

Getting access to the window object will be the hard part, and I think it's only possible if the iframes are from the same domain as your page.

2
  • 1
    Is this method more browser compatible than the one using the Event object? I presume it is. Commented Aug 22, 2017 at 10:06
  • 5
    @OgierSchelvis No idea, it was half a decade ago. I think if you're needing to do this then you're kinda already in a realm of weirdness that I don't want to think about anymore.
    – david
    Commented Aug 22, 2017 at 10:55
3

Document.createEvent() and Event.initEvent() are deprecated.

Use Event constructor for now:

const evt = new Event('build')
window.dispatchEvent(evt)
0

If you are thinking about iframe slideshow where you want to display content from other pages, then you should check out this tutorial (instead of images simply add urls): http://www.2webvideo.com/blog/easily-create-slideshow-with-iframe-tag

1
  • This is basically what I want to do, but swapping urls for the images does no good. I do not want to reload them everytime, I want to be able to fire the onload event after display, basically refreshing the page without calling the server.
    – user999684
    Commented Mar 10, 2012 at 0:52
0

Using this tutorial you can use:

document.getElementById("$FRAME_ID$").contentDocument.location.reload(true);

Then simply add new ID or CLASS for each element, see: https://stackoverflow.com/a/2064863/1097415

2
  • Won't that force another call to the server though? That is what I am trying to avoid.
    – user999684
    Commented Mar 10, 2012 at 1:07
  • It will call the page content again in iframe (it will reload the content of iframe).
    – richardev
    Commented Mar 10, 2012 at 1:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.