How do I detect whether the document is in a frame or in an iframe?

Posted on 2005-04-19
Last Modified: 2008-03-03
Question by:zhghl
    LVL 22

    Assisted Solution

    this will tell you if the document is in a frame
    LVL 6

    Assisted Solution

    by:Samer Chidiac
    i'm not sure how you're going to use the information once you knew it ..

    can you clarify why you want to know it...  ( like r you going to write a script to detect that and write an action to redirect users or something ? )

    usually the framed page is a page that is formed from different pages assembled in together using frames, and the Main Framed Page is very simple can cannot include but frames (it even don't have a <Body> Tag) while the sub pages contains the Content .. while an iframe can be embedded in a normal page.. and can be floating..

    Take Care,


    Samer Chidiac
    Microsoft MVP - ASP /
    LVL 2

    Assisted Solution

    Hi you can add this into the <body> tag of any page and it will take you to the correct page.  You can change the url of course.  This is fix is needed if a search engine arrives at a parentless page and you want to put the navigation back.

    Look at the parent.frames.length aspect

    <body topmargin="0" leftmargin="0" OnLoad="if(parent.frames.length==0)top.location='';">

    Shout if you need more help on what to do after you redirect.

    LVL 53

    Accepted Solution

    in the head of the page
    <script type="text/javascript">
    function testSelf()
       return (arr.length==0) ? 'iframe' : 'frame';

    In the body tag of the framed page you fire it with:

    <body onload="if (self != top) alert(testSelf());"


    Author Comment

    if( top != self ){
         if( window.frameElement.tagName== "IFRAME" ){
          alert( "iframe" );      
          alert( "frame" );      
       alert( "page" );

    thanks a lot

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
    This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now