How to add body class in Wordpress if the page is being viewed in an iframe?

From the post How to identify if a webpage is being loaded inside an iframe or directly into the browser window? I understand you can detect whether or not your page is being viewed through a frame or not.

Using a WordPress site. I'd like to append a body class tag to my site dependent on it's view. I'm doing this so I can tweak the styling. I understand this can't be done via PHP and you need to use JavaScript.

Can somebody walk me through an example of how I can add my body class with this?

if (top === self) { not iframe -do nothing } else { in a frame - add body class }

Thanks in advance for the hand holding.

Answers


You can do this with jQuery by adding this snippet to your document head. This will add to the body class attribute if the page with javascript code is being embedded through an iframe:

<script type="text/javascript">
$(function() {
  var isInIFrame = (window.location != window.parent.location) ? true : false;
  if(isInIFrame) {
    $('body').addClass('iframe');
  }
});
</script>

You can view some more source code for this answer on Github


Need Your Help

How do I remove south from a django project

python django django-south

I installed south and tried a few changes using it, which didn't exactly work out the way I wanted it to. Thankfully, my data is safe but locked into south. I want to remove south and use syncdb no...

ViewAnimator setting next view problem?

android view

I'm using ViewAnimator to switch between views...