How to hide an iframe if no content is returned wi

2019-03-02 05:52发布

问题:

I am trying to make a content area with a specific size, but I want nothing to be displayed if the returned result from the api is empty.

This is the code for the html:

<div class="myclass">
        <iframe frameborder="0" src="http://localhost:1000/example"></iframe>
</div>

I'm calling an API that sometimes might return a null result. Javascript is off the table. I've tried to use a css restraint like this:

.myclass {
max-width: 1060px; 
max-height: 392px;

  & > iframe {
      min-height: 0;
      min-width: 0;
      max-width: 1060px;
      max-height: 392px;
    }

   & > iframe:empty {
    display: none;
 }
}

The behavior for the css is: the iframe is hidden all the time, although I have content inside it. Also if the iframe is like this:

<div class="myclass">
        <iframe frameborder="0" src="http://localhost:1000/example">
        <!--notice white-space here-->
        </iframe>
</div>

The css will not see the iframe as empty.

回答1:

I actually made it happen without javascript.

But you need to create a proxy that generates the css.

If below is not a possibility then all bets seem off. Good luck!

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        @import url('iframecheck.asp?url=http://www.example.com');
        iframe {
            width:1000px;
            height:400px;
        }
    </style>
</head>
<body>
<iframe src="http://www.example.com"></iframe>
</body>
</html>

The iframecheck contains code that checks whether the url has empty response, if it does it returns css like this:

iframe {
    display:none;
}

Which will automatically override the other iframe style.

Don forget to force the text/css content type header if you do.

<%response.ContentType="text/css"%>