如果内容为空,如何隐藏iframe [英] how to hide an iframe if content is empty

查看:161
本文介绍了如果内容为空,如何隐藏iframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iframe,其中显示了来自外部网址的某些内容.如果没有要显示的内容(即为空),我想隐藏iframe.请让我知道该怎么做.

I am using iframe in which I am displaying some content from external url. I want to hide the iframe if there is no content to display (i.e empty). please let me know how to do this.

推荐答案

如果您要检查iframe中来自外部url的内容是否为空,并且iframe不是跨域的,则可以检查其中是否存在body标记iframe的如果存在,则加载某些内容.好吧,如果您可以使用jQuery,请检查它的length属性.这是跨浏览器兼容的.如果为零,则不存在.

If you want to check content from external url inside iframe is empty, and the iframe is not cross-domain you could check for the existence of the body tag inside of the iframe. If it exists, then something loaded. Well if you can use jQuery, check it's length property. This is cross-browser compatible. If it's zero, it doesn't exist.

代码:

if($("#iframeid").contents().find("body").length) {
    // some html page loaded in iframe
}

如果iframe是跨域的,则您将受到同源政策.否则,它将起作用.

If the iframe is cross-domain, you will be blocked by the same-origin policy. Otherwise this will work.

来源:如何检查iframe是否为空/空/未定义?

这篇关于如果内容为空,如何隐藏iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆