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.

user999684
  • 705
  • 2
  • 10
  • 23

5 Answers5

37

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)
Jameal G
  • 324
  • 3
  • 10
Martin Wantke
  • 4,287
  • 33
  • 21
  • 4
    Doesn't work on that, uh, "browser" named Internet Explorer. – alecov May 31 '16 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 Jun 06 '18 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.

david
  • 17,925
  • 4
  • 43
  • 57
  • 1
    Is this method more browser compatible than the one using the Event object? I presume it is. – Ogier Schelvis Aug 22 '17 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 Aug 22 '17 at 10:55
3

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

Use Event constructor for now:

const evt = new Event('build')
window.dispatchEvent(evt)
Valery Bugakov
  • 355
  • 1
  • 10
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

richardev
  • 976
  • 1
  • 10
  • 33
  • 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 Mar 10 '12 at 00: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

Community
  • 1
  • 1
richardev
  • 976
  • 1
  • 10
  • 33