Hi all, I have a facebook page tab application that is hosted on my server.
If I navigate to the FQD location the page will load as you would expect.

What I would like to do is detect if the page is being loaded inside an iframe, ie. being displayed from inside facebook, from the page tab application. ie. https://www.facebook.com/pagename/app_257600168717381

If the page is being displayed from outside of facebook, ie. domainname.com/dir/facebook-app is in the address bar of the browser, I would like to auto redirect the page to the facebook page tab application url.

I have found the following code, but my page just loops ?

    <script type="text/javascript">
    top.location.href = "https://www.facebook.com/pagename/app_257600168717381";
    </script>

I have a variable called $token that is the full https://www.facebook.com/pagename/app_257600168717381 path
How can I edit the script to first detect if the page is being loaded within a iframe and secondly auto redirect the header location, using the $token variable if the page is being loaded from the address bar url.

Ok, I have found a working script. Just thought I would post the answer I found to help others.

<script type="text/javascript">

    var framespage="<?php if($token){echo $token; }?>" //This is the frames page for your page.

    //No need for editing below here.
    if (top.location==document.location){
      top.location=framespage;
    }else{
      var parent_location=parent.location.href;
      var str_beginning=parent_location.length-framespage.length;
      if (parent_location.substring(str_beginning, parent_location.length)!=framespage){
        parent.location=framespage;
      }
    }

</script>

Hope this helps others.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.