0

I would like to remove all images that are to be displayed on an HTML file, which contains a refreshed iframe.
Is there possibly a method using CSS or JavaScript that would do so?

The page that loads the iframe uses the code below.

function reloadIFrame() {
  console.log('reloading..');
  document.getElementById('iframe').contentWindow.location.reload();
}

window.setInterval(reloadIFrame, 3000);
body {
  margin: 0px;
}
h1 {
  font-family: arial;
  font-size: 50%;
}
iframe {
  width: 100%;
  height: 800px;
}
div.top {
  background-color: rgba(2, 109, 8, 0.83);
  height: 1%;
  width: 100%;
}
<!DOCTYPE html>
<html>
<head>
  <link rel="shortcut icon" href="news.png" type="image/x-icon" />
  <title>Chat</title>
</head>
<body>
  <div class="top">
    <img src="news.png" alt="newsicons" height="31" width="31">
    <h1>News</h1>
  </div>

  <iframe id="iframe" src="http://www.bbc.com/news/world"></iframe>
</body>
</html>
Flygenring
  • 3,517
  • 28
  • 37
user98734
  • 11
  • 1

1 Answers1

0

You can do this using javascript. Check updated snippet below..

if(document.getElementById('iframe')) {
  var images = document.getElementsByTagName('img');
  for (i = 0; i < images.length;i++ ) {
    images[i].style.display = "none";
  }
}
<div class="top">
        <img src="news.png" alt="newsicons" height="31" width="31">
        <h1>News</h1>
    </div>

    <iframe id="iframe" src="http://www.bbc.com/news/world"></iframe>
Super User
  • 8,642
  • 3
  • 24
  • 43
  • I think this may not work due to the cross-origin policy. It should work if the iframe source is on the same domain though. – ralixyle Aug 30 '17 at 08:15
  • The same origin policy won't have any effect on this code: It doesn't event attempt to access the document in the frame. – Quentin Aug 30 '17 at 08:23
  • @Quentin true, but assuming it'll access the iframe html content.. :-) – ralixyle Aug 30 '17 at 08:46
  • @Nivin — That's not a good assumption. The code clearly shows that it will not do that. There's no attempt to access the `contentDocument` (or any other property) of the `iframe` element in this code at all. This code checks to see if an element with the id *iframe* **exists** as a precondition for hiding all the images in the **current** document (but not any document loaded into the frame). – Quentin Aug 30 '17 at 08:48
  • @Quentin yes... I meant iframeId.contentWindow.document.body.getElementsByTagName('img') – ralixyle Aug 30 '17 at 08:50
  • @Nivin — What does that have to do with this answer? – Quentin Aug 30 '17 at 08:52
  • @Nivin OP is asking to remove all images in html files which has an iframe not inside iframe, that's why i written this code using javascript & this is working fine in snippet. – Super User Aug 30 '17 at 08:56