Search code examples
javascriptasp.netfacebookiframedetection

How to understand your pages are being requested under facebook or not inside masterpage asp.net


I am going to add my website to the facebook as an application. Facebook do not allow 3 rd party ads so i have to remove them. I want to remove ads for only facebook visitors. So i want to understand whether page is being called under facebook as an iframe or not. How can i do that with javascript ? Thank you.

I can also use jquery.

asp.net 4.0


Solution

  • The following detects if your page is viewed in an iframe:

    if (window.location != window.parent.location){
          //you are in an iframe
    }
    else {
          //you are NOT in an iframe
    }
    

    [EDIT]
    If you want also to specify that the iframe is in facebook then you could try this:

    if(window.name != "") {
      //We are on Facebook
    } 
    else
    {
      //We are just in the normal browser window
    }
    

    I haven't tested this, just came across a few weeks ago here