Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
在 Tornado 中,您可以在 HTML 中执行 if 语句,例如{% if true %} do stuff {% end %}. 我想检查页面是否在 iframe 中。
{% if true %} do stuff {% end %}
在 Javascript 中,它会是这样的:if (top === self) { not in a frame } else { in a frame }
if (top === self) { not in a frame } else { in a frame }
我怎样才能在 Tornado 中做到这一点?
Javascript 可以访问浏览器上下文,但模板系统只能访问请求对象。
如果您控制有问题的 iframe 的创建,例如,如果这发生在您网站的另一部分,您可能能够将 get 参数传递给模板系统或其他东西......但通常这是您拥有的东西与javascript有关。将 javascript 直接添加到您的模板或(更好地)包含一个 javascript 文件。您可以在模板中公开页面的 iframed 和非 iframed 版本,并让 javascript 在浏览器中选择要显示的版本。