4

I need to display a link if the current page is being viewed in parent browser window, or display another link if window is being viewed inside an iframe. How can i do that via php? Something like:

if (self == top)
        echo '<span><a href="./" >Link1</a></span>';
else
        echo '<span><a href="./index.php">Link2</a></span>';

edit: since it cant be done with php, i still looking for similar solution, maybe JS, can someone pls tell me how?

Final edit: The answer:

echo '  
<script type="text/javascript">
    if(window === top) {
document.write("<span><a href=\"./\">go-to-frame</a></span>");
    }
    else {
document.write("<span><a href=\"./index.php\">go-to-top</a></span>");
    }
</script>
';

thank you all.

Lucas Matos
  • 1,112
  • 5
  • 25
  • 42
  • It's probably a lot easier to pass a query string based on whether you're using it in an iframe, but if that's not possible you'll need to use JavaScript to rewrite your links. – cmbuckley Mar 02 '12 at 00:44
  • well, playing around with both answers i figured out how to do this. Since i cant mark the 2 answers as correct i wont mark none. Is that ok guys? or i should do something else about it? Anyway, i edited the first topic with the correct answer. – Lucas Matos Mar 02 '12 at 01:36
  • Possible duplicate of [How to identify if a webpage is being loaded inside an iframe or directly into the browser window?](http://stackoverflow.com/questions/326069/how-to-identify-if-a-webpage-is-being-loaded-inside-an-iframe-or-directly-into-t) – Mark Amery Dec 24 '16 at 16:14

3 Answers3

23

You can't do this with PHP; it's a server side language, not a client side one. Because it's up to the client how it handles windows, the server doesn't know anything about this. You could send something back in an AJAX request then reload the page, but this is a messy, horrible solution.

To detect if you're in the top level window, you need to do this:

if(window.top == window.self) {
    // Top level window
} else {
    // Not top level. An iframe, popup or something
}

You were very close with the example you gave in your question. window.top is the top-most window of the stack, and window.self is the window the current JS and DOM is in.

Bojangles
  • 99,427
  • 50
  • 170
  • 208
  • Ok, but i have limited knowledge, how can i insert this JS in my php script? – Lucas Matos Mar 02 '12 at 00:46
  • 1
    @LucasMatos You cannot insert JavaScript into PHP, unless you *echo* it. – MacMac Mar 02 '12 at 00:47
  • 1
    @Lucas You need to put that JavaScript in `` tags in your HTML. You should do some research on how to include JavaScript into a page. – Bojangles Mar 02 '12 at 00:49
  • something like `echo '';` – Lucas Matos Mar 02 '12 at 00:51
0

There is no way to detect that about the environment from within PHP.

However, you can detect it from within Javascript (usually).

Example:

(in your HTML somewhere)

<span><a id="MyDynamicLink" href="./">Link1</a></span>

(In your script at the end of the <body>:

<script type="text/javascript">
    if(window === top) {
        function () {
            //this grabs the element from the document tree
            var link = document.getElementById('MyDynamicLink');

            //this sets the 'href' to './index.php'.
            link.setAttribute('href', './index.php');

            //this sets the text inside the link to be 'Link2'
            link.innerHTML = 'Link2';
            //or whatever else you really wanted to do in this situation
        }();
    }
</script>

The Javascript is of course trivial to get around. But if your goal is to help rather than obstruct your users, that shouldn't be an issue.

Jeremy
  • 533
  • 3
  • 10
  • that looks like a nice solution, but i couldnt figure what what you are doing there.. couldnt make that work. Link1 and Link2 are the text anchors not real link (urls). – Lucas Matos Mar 02 '12 at 00:59
  • its not working, its heading me to same destination **./** whenever the page is being viewed in a frame or not, and the text link is the same either **Link1** – Lucas Matos Mar 02 '12 at 01:14
-2

The answer is

echo '  
<script type="text/javascript">
    if(window === top) {
document.write("<span><a href=\"./\">go-to-frame</a></span>");
    }
    else {
document.write("<span><a href=\"./index.php\">go-to-top</a></span>");
    }
</script>
';
Lucas Matos
  • 1,112
  • 5
  • 25
  • 42