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

前端 未结 16 2336
攒了一身酷
攒了一身酷 2020-11-22 00:28

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

相关标签:
16条回答
  • 2020-11-22 00:58

    Write this javascript in each page

    if (self == top)
      { window.location = "Home.aspx"; }
    

    Then it will automatically redirects to home page.

    0 讨论(0)
  • 2020-11-22 01:00

    I'm not sure how this example works for older Web browsers but I use this for IE, Firefox and Chrome without an issue:

    var iFrameDetection = (window === window.parent) ? false : true;
    
    0 讨论(0)
  • 2020-11-22 01:01

    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.

    0 讨论(0)
  • 2020-11-22 01:01
    if (window.frames.length != parent.frames.length) { page loaded in iframe }
    

    But only if number of iframes differs in your page and page who are loading you in iframe. Make no iframe in your page to have 100% guarantee of result of this code

    0 讨论(0)
  • 2020-11-22 01:04

    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.

    0 讨论(0)
  • 2020-11-22 01:06

    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;
    }
    
    0 讨论(0)
提交回复
热议问题