How to identify if a webpage is being loaded inside an iframe or directly into the browser window?
Browsers can block access to window.top
due to same origin policy. IE bugs also take place. Here's the working code:
function inIframe () {
try {
return window.self !== window.top;
} catch (e) {
return true;
}
}
top
and self
are both window
objects (along with parent
), so you're seeing if your window is the top window.
Detect iFrame embedding in Javascript
Looking at frame length breaks down generally if page A itself has frames (I know this might not be the case for this specific instance). The more reliable and meaningful test would be:
if (window!=window.top) { /* I'm in a frame! */ }
Foolproof way to detect if this page is INSIDE a cross-domain iframe
First check if you are IFramed.
window.self !== window.top
If you are IFramed, then your referrer is your parent frame url.
document.referrer
From this url you should be able to detect if you want to branch your code.
How to prevent a web-page from knowing that it is loaded inside an iframe?
There is no way to get this information. The reason that websites should always be able to know they're in an iframe is for security reasons.
It allows for things like frame-busting, where a website stops itself from being displayed or redirects to the site itself.
If a website were to be shown in an iframe without knowledge of this, I could overlay a separate form element and use this in phishing attacks.
That would be a serious security issue.
Check if parent window is iframe or not
This is true if a window is not a frame/iframe:
if(self==top)
If you like to see if the parent window of the given window is a frame, use:
if(parent==top)
It's a simple comparision of top
(the most top window of the window hierarchy) and another window object (self
or parent
).
How can I tell if a page is being served to an iframe?
if (window==window.top) { /* I'm not in an iframe */ }
Very simple javascript.
Related Topics
Jquery Animate Backgroundcolor
What Are Valid Date Time Strings in JavaScript
Attach Event to Dynamic Elements in JavaScript
What Is the "Right" Json Date Format
JavaScript For Detecting Browser Language Preference
How to Add a Class to a Given Element
Load and Execute Order of Scripts
Difference Between Call and Apply
How to Get the Caret Column (Not Pixels) Position in a Textarea, in Characters, from the Start
How to Get the Current Date in JavaScript
Disable/Enable an Input With Jquery
What Is Spreadelement in Ecmascript Documentation? Is It the Same as Spread Syntax At Mdn
How to Do Case Insensitive String Comparison
When Is JavaScript'S Eval() Not Evil