user999684
user999684

Reputation: 761

Force a window.onload event in javascript

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.

Upvotes: 14

Views: 39331

Answers (5)

Martin Wantke
Martin Wantke

Reputation: 4571

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)

Upvotes: 38

richardevcom
richardevcom

Reputation: 1058

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

Upvotes: 0

Valery Bugakov
Valery Bugakov

Reputation: 355

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

Use Event constructor for now:

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

Upvotes: 3

david
david

Reputation: 18288

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.

Upvotes: 14

richardevcom
richardevcom

Reputation: 1058

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

Upvotes: 0

Related Questions