The Oddler
The Oddler

Reputation: 6708

Refresh iframe when looking away (from within iframe itself)

I have a page that embeds an iframe with a Unity game in it, the game isn't loaded until the user actually clicks in the iframe. This to make sure the page loads quickly, and no games are running that aren't being played (there can be multiple games on one page).

Now I was wondering, is it possible to make the iframe refresh when the user scrolls away? So the game stops running and the again waits for a click. Is it possible by only changing the page being shown by the iframe? (So not the page that has the iframe.)

Note: I have limited control over the actual page embedding the iframe, that's why it would be nice if this could be done by the page being shown by the iframe.

Edit: I found this for refreshing: self.location.reload();, now I only need to be able to detect if the iframe is in view from within the iframe.

Upvotes: 0

Views: 102

Answers (2)

Jackson
Jackson

Reputation: 3518

If the iframe src is within the same domain as the parent window then it would be possible. In your game page, you could detect a scroll event in the parent window like this:

var parent = window.parent;
parent.onscroll = function (e) {  
   // the parent window has scrolled,
   // you can pause the game here
} 

Then you can detect a the user clicking on the iframe by just adding a click event.

window.onclick = function() {
   // user has clicked on the iframe,
   // you can initialise the game now
}

Upvotes: 0

Tui Popenoe
Tui Popenoe

Reputation: 2114

You cannot detect if the iframe is in the viewport of an external site, because you cannot access the document of the parent domain from within the iframe. That would be needed to determine the position of the iframe element on the page.

Upvotes: 1

Related Questions