Skip to main content

How to identify if a webpage is being loaded inside an iframe or directly into the browser window?


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?



Source: Tips4allCCNA FINAL EXAM

Comments

  1. 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.

    ReplyDelete
  2. RoBorg is correct, but I wanted to add a side note.

    In IE7/IE8 when Microsoft added Tabs to their browser they broke one thing that will cause havoc with your JS if you are not careful.

    Imagine this page layout:

    MainPage.html
    IframedPage1.html (named "foo")
    IframedPage2.html (named "bar")
    IframedPage3.html (named "baz")


    Now in frame "baz" you click a link (no target, loads in the "baz" frame) it works fine.

    If the page that gets loaded, lets call it special.html, uses JS to check if "it" has a parent frame named "bar" it will return true (expected).

    Now lets say that the special.html page when it loads, checks the parent frame (for existence and its name, and if it is "bar" it reloads itself in the bar frame. e.g.

    if(window.parent && window.parent.name == 'bar'){
    window.parent.location = self.location;
    }


    So far so good. Now comes the bug.

    Lets say instead of clicking on the original link like normal, and loading the special.html page in the "baz" frame, you middle-clicked it or chose to open it in a new Tab.

    When that new tab loads (with no parent frames at all!) IE will enter an endless loop of page loading! because IE "copies over" the frame structure in JavaScript such that the new tab DOES have a parent, and that parent HAS the name "bar".

    The good news, is that checking:

    if(self == top){
    //this returns true!
    }


    in that new tab does return true, and thus you can test for this odd condition.

    ReplyDelete
  3. 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:

    (window !== window.top) : false
    (window.self !== window.top) : true


    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:

    console.log(window.frames.length + ':' + parent.frames.length);


    Without iframes this prints 0:0, in a top-level window containing one frame it prints 1:1, and in the only iframe of a document it prints 0: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.

    ReplyDelete
  4. Since you are asking in the context of a facebook app, you might want to consider detecting this at the server when the initial request is made. Facebook will pass along a bunch of querystring data including the fb_sig_user key if it is called from an iframe.

    Since you probably need to check and use this data anyway in your app, use it to determine the the appropriate context to render.

    ReplyDelete
  5. Use this javascript function as an example on how to accomplish this.

    function isNoIframeOrIframeInMyHost() {
    // Validation: it must be loaded as the top page, or if it is loaded in an iframe
    // then it must be embedded in my own domain.
    // Info: IF top.location.href is not accessible THEN it is embedded in an iframe
    // and the domains are different.
    var myresult = true;
    try {
    var tophref = top.location.href;
    var tophostname = top.location.hostname.toString();
    var myhref = location.href;
    if (tophref === myhref) {
    myresult = true;
    } else if (tophostname !== "www.yourdomain.com") {
    myresult = false;
    }
    } catch (error) {
    // error is a permission error that top.location.href is not accessible
    // (which means parent domain <> iframe domain)!
    myresult = false;
    }
    return myresult;
    }

    ReplyDelete
  6. There is now also an official way to do it over the Graph API: http://developers.facebook.com/blog/post/586/

    ReplyDelete
  7. It's an ancient piece of code that I've used a few times:

    if (parent.location.href == self.location.href) {
    window.location.href = 'https://www.facebook.com/pagename?v=app_1357902468';
    }

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex