Reputation: 2123
My problem is that I have two html files for example say 1.html and 2.html. The contents of the files are 1.html It consists of the Iframe. The source of the Iframe is 2.html. 2.html It is a sample html page.
My question is that I want to check whether the 2.html is loaded on an Iframe or loaded on a separate browser directly without putting it inside an Iframe. The checking has to be done from 2.html only.
Any suggestions friends. Thanks in advance.
Upvotes: 0
Views: 1043
Reputation: 714
if (top === self) { not in a frame } else { in a frame }
Top and self are both window objects (along with parent), so you're seeing if your window is the top window.
Credit to this. Hope that helps.
Upvotes: 0
Reputation: 79069
Bind a function inside the iframe's onload
event and set a loaded variable on the parent page
On the iframe
window.onload = function() {
parent.iframeLoaded = true;
}
On the parent page, just declare the variable to hold the value
var iframeLoaded = false;
Now you can check this var when you need from the parent page like
if(iframeLoaded) {
// Do something
}
Upvotes: 0
Reputation: 158
when loaded in iframe the window.parent points to the parent window, however when loaded in a separate window window.parent points to window itself:
var loadinInIframe = window.parent != window;
Upvotes: 5
Reputation: 351
with javascript you can check using document.location if window.parent.location and window.location, if parent location is not your app location then you might be iFramed
Upvotes: 0