简体   繁体   中英

iFrame alternative for getting cross-domain URL

I'm creating a web-based game. As a part of it, the player must go to the home page of a website, and follow a series of link until he reaches to the "destination page" in the minor time possible. Think of it as a sort of link treasure hunt.

In order to control the game, I created a page with a javascript timer on the top, and an iFrame showing the website to surf (which is not mine and I have no control over it).

However this has a huge fault: it is not possible to detect the current cross-domain URL of the iFrame, so my page cannot know when the user has arrived to the destination page and stop the game.

Is there any alternative way I could achieve this?

Is there any alternative way I could achieve this?

No, there is not.

The Same Origin Policy completely prevents you from accessing properties of any window object that displays content from another domain.

(If one was a subdomain of the other, then document.domain would help. But from your problem description it doesn't sound like that was the case.)


And besides the technical issue: Generating traffic and clicks (that will skew their statistics/analytics) on a site that you don't own, just for the purpose of being able to present your own game, would be rather unfair (aka a “dick move”.)

You can wait upto the contentWindow of the iFrame is resolved and you have a reference to the iFrame's window, after you changed the source pragmatically. once you have that, you can add a onload handler on iFrame's <body> and inject some postMessage call inside that handler, so once the body is loaded of the iFrame's window it wil call postMessage from the onload handler of body and it will notify your outer window, of-course you have to add message listener in outer window using window.addEventListener("message", callback)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM