javascript 如何通过 php 检查当前窗口是否为“顶部”(或“父”)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9526419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can i check if the current window is "top" (or "parent") via php?
提问by Lucas Matos
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:
如果在父浏览器窗口中查看当前页面,我需要显示一个链接,如果在 iframe 内查看窗口,则需要显示另一个链接。我怎样才能通过 php 做到这一点?就像是:
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?
编辑:因为它不能用 php 完成,我仍在寻找类似的解决方案,也许是 JS,有人可以告诉我如何吗?
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.
谢谢你们。
采纳答案by Lucas Matos
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>
';
回答by Bojangles
You can'tdo 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.
你不能用 PHP 做到这一点;它是一种服务器端语言,而不是客户端语言。因为这取决于客户端如何处理窗口,所以服务器对此一无所知。您可以在 AJAX 请求中发回一些内容,然后重新加载页面,但这是一个混乱、可怕的解决方案。
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.
你非常接近你在问题中给出的例子。window.top
是栈顶window.self
的窗口,是当前 JS 和 DOM 所在的窗口。
回答by Jeremy
There is no way to detect that about the environment from within PHP.
无法从 PHP 内部检测到有关环境的信息。
However, you can detect it from within Javascript (usually).
但是,您可以从 Javascript 中检测它(通常)。
Example:
例子:
(in your HTML somewhere)
(在你的 HTML 中的某个地方)
<span><a id="MyDynamicLink" href="./">Link1</a></span>
(In your script at the end of the <body>
:
(在您的脚本末尾<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.
Javascript当然是微不足道的。但是,如果您的目标是帮助而不是阻碍用户,那应该不是问题。