1

I want to remove specific html if my page is shown in an iframe. What I want to do in my (html) code:

<body>
<!--Some random html here-->
<script>    
  if(self==top) { /* <- This detects that the page is not displayed in an iframe*/
    /*-----The html that I put here should only show if not in an iframe!-----*/
  }
</script>
<!--Some random html here-->
</body>
6
  • 1
    That's nice. Good luck figuring it out. Did you have a question? Commented Apr 13, 2015 at 18:15
  • You put javascript in the script tag, not html. Commented Apr 13, 2015 at 18:15
  • 1
    You can add/remove a class on the content so it does not appear. Commented Apr 13, 2015 at 18:16
  • Please provide an example of the html you wish to be hidden Commented Apr 13, 2015 at 18:18
  • @epascarello Thanks. If I do as you say to my div-container, what css can I add to totally remove the content inside (and maybe div too)? Commented Apr 13, 2015 at 18:25

2 Answers 2

2

The best way to do this would be to put the html that might be hidden inside an element, and then show/hide that element based on whether you're in an iframe or not.

<head>
    <script>    
       if(self===top) { /* <- This detects that the page is not displayed in an iframe*/
          document.getElementById('HideIfIframe').style.display = 'block';
       }
    </script>
</head>
<body>
    This will always show.
    <div id="HideIfIframe" style="display: none;">This will only show when not in an iFrame</div>
    <!--Some random html here-->
</body>

You'll probably need to put the JS code in a function and execute it on page load, to make sure that the div is available, but that's the basic idea.

Sign up to request clarification or add additional context in comments.

2 Comments

I think a class would be better IF Rasmus Myhrberg wants to hide multiple elements.
@RasmusMyhrberg getElementsByClassName() If you use it you might want to use a for loop to apply the new styles to each elements of the class you get.
0

Using the if (self==top) method, or, better if (self==parent), you can check if your page is in an <iframe> and remove the desired element afterwards.

To implement this behavior you'll need some distinguishable id or class to find the elements to remove, and also load the script after them, so maybe at the end of your body.

Here's an example:

In your HTML page you'll add some class to the elements you don't want to be shown in a frame, like this:

<body>
    <div class="noframe"></div>
    <!-- I don't want this to be shown in a frame! -->

    <script src="script.js"></script>
</body>

In your script.js file you'll simply remove the elements:

if (self != parent) {
    var toRemove = document.querySelectorAll('.noframe');

    // Remove the elements you don't want in the iframe
    for (var i=0; i < toRemove.length; i++) {
        toRemove[i].parentElement.removeChild(toRemove[i]);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.