to check parent window is iframe or not

How can I tell from a page within an iframe, if the parent itself is also within an iframe?

Explaination:

  • Using iframe with local files in Chrome
  • How to add result of html page which contains jquery scripts in iframe
  • Should I include the same library on different iFrames?
  • WinJS - Ignore iframe javascript errors
  • How can I pass attribute value from iframe to parent in Javascript?
  • Why does Internet Explorer open form submission in a new window and not in a dynamically inserted iframe?
  • My home page home.html contains an iframe

    <iframe src="sample.html"></iframe>
    

    I need to detect if home.html (ie: parent of sample.html) is within an iframe.

    Codes in sample.html

    if(self==window)
    {
        alert('home.html is not in iframe');
    }
    else
    {
        alert('home.html is in iframe');
    }
    

    My Question is not a duplicate. It’s a different case. Help me.

  • Detect when an iframe is loaded
  • How to prevent execution of Greasemonkey function in subwindows?
  • Refresh iframe when hosted on different server
  • lazy load iframe (delay src http call) with jquery
  • call javascript function from outside an iframe
  • UIWebView producing about:blank requests when iframe appended
  • 3 Solutions collect form web for “to check parent window is iframe or not”

    if(self==top)
    

    is true if a window is not a frame/iframe

    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(what self or parent are)

    Check if window.frameElement is not null and see if its nodeName property is “IFRAME”:

    var isInIframe = window.frameElement && window.frameElement.nodeName == "IFRAME";
    
    var isInIFrame = (window.location != window.parent.location);
    if(isInIFrame==true){
        // iframe
    }
    else {
        // no iframe
    }