I am writing an iframe based facebook app. Now I want to use the same html page to render the normal website as well as the canvas page within facebook. I want to know if I can determine whether the page has been loaded inside the iframe or directly in the browser?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
The accepted answer didn't work for me inside the content script of a Firefox 6.0 Extension (Addon-SDK 1.0): Firefox executes the content script in each: the top-level window and in all iframes.
Inside the content script I get the following results:
The strange thing about this output is that it's always the same regardless whether the code is run inside an iframe or the top-level window.
On the other hand Google Chrome seems to execute my content script only once within the top-level window, so the above wouldn't work at all.
What finally worked for me in a content script in both browsers is this:
Without iframes this prints
0:0
, in a top-level window containing one frame it prints1:1
, and in the only iframe of a document it prints0:1
.This allows my extension to determine in both browsers if there are any iframes present, and additionally in Firefox if it is run inside one of the iframes.
I'm using this:
Browsers can block access to
window.top
due to same origin policy. IE bugs also take place. Here's the working code:top
andself
are bothwindow
objects (along withparent
), so you're seeing if your window is the top window.Use this javascript function as an example on how to accomplish this.
It's an ancient piece of code that I've used a few times:
Write this javascript in each page
Then it will automatically redirects to home page.