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? Commented Mar 10, 2012 at 0:45
  • The javascript would be in the parent which is running the show. Commented Mar 10, 2012 at 0:47
  • One more question - plain ol' JavaScript, or jQuery, etc.? Commented Mar 10, 2012 at 0:49
  • stackoverflow.com/questions/1654017/… 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. Commented Mar 10, 2012 at 1:03

5 Answers 5

39

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Doesn't work on that, uh, "browser" named Internet Explorer.
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."
13

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 Comments

Is this method more browser compatible than the one using the Event object? I presume it is.
@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.
3

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

Use Event constructor for now:

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

Comments

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 Comment

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.
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 Comments

Won't that force another call to the server though? That is what I am trying to avoid.
It will call the page content again in iframe (it will reload the content of iframe).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.