DEV Community

Cover image for How to disable iFrames after a few seconds like they do on codePen.
Jochem Stoel
Jochem Stoel

Posted on • Updated on

How to disable iFrames after a few seconds like they do on codePen.

Most of us use CodePen, a nice HTML/CSS/JS playground to create and share snippets. You have probably noticed that iFrame previews in search results suddenly stop working after 3 or 4 seconds.

I was curious how this works (as far as I knew this was not possible) and came up with a working solution.

You need to

  • Disable animations
  • Disable audio/video elements
  • Wrap this up and set a timeout
  • Inject it into every iFrame

Don't worry, it is pretty easy and straightforward.

Disable all CSS animations

The following will find and iterate through all the nodes rendered in the DOM. For each HTMLElement it will set the CSS properties to 'paused'.

function disablewebkitAnimations() {
    var nodes = document.querySelectorAll('*')
    for (var i = 0; i < nodes.length; i++) {
        style = nodes[i].style
        style.webkitAnimationPlayState = 'paused'
        document.body.className = 'paused'
    }
}
Enter fullscreen mode Exit fullscreen mode

Disable audio/video

The following function will select all the elements of type type, iterate through them and simply call the pause() method.

function pauseElementTypes(type) {
    let nodes = document.querySelectorAll(type)
    for (var i = 0, els = document.getElementsByTagName(type); i < els.length; i++) {
        els[i].pause();
    }
}
Enter fullscreen mode Exit fullscreen mode

Note: This will not stop Audio and Video elements not rendered in the DOM. This means if you create a new HTMLAudioElement then play it but don't append it to your document, it will keep playing. This is why some pens on CodePen in results still manage to make sound sometimes.

Wrapping it up

A simple function that does everything we just described.

function iFrameDisable() {
    disablewebkitAnimations()
    pauseElementTypes("audio")
    pauseElementTypes("video")
}

setTimeout(iFrameDisable, 4000) // execute after 4 seconds
Enter fullscreen mode Exit fullscreen mode

Inject

Basically, on CodePen they inject the the following code in every iFrame of their search results. (the function names may differ but it is conceptually the same)

<script>
function disablewebkitAnimations() {
    var nodes = document.querySelectorAll('*')
    for (var i = 0; i < nodes.length; i++) {
        style = nodes[i].style
        style.webkitAnimationPlayState = 'paused'
        document.body.className = 'paused'
    }
}

function pauseElementTypes(type) {
    let nodes = document.querySelectorAll(type)
    for (var i = 0, els = document.getElementsByTagName(type); i < els.length; i++) {
        els[i].pause();
    }
}

function iFrameDisable() {
    disablewebkitAnimations()
    pauseElementTypes("audio")
    pauseElementTypes("video")
}

setTimeout(iFrameDisable, 4000)
</script>
Enter fullscreen mode Exit fullscreen mode

Additionally, you can extend this to also disable webkitGetUserMedia of the window navigator but I'll leave that up to you. (CodePen doesn't do this yet either)

I am Jochem Stoel. If this post was useful to you or you have something to say then I invite you to leave a response or look at some of my other content.

Top comments (3)

Collapse
 
kspeakman profile image
Kasey Speakman

Hi Joel, could you mention a bit about why you want to disable iframes? Just curious.

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
kspeakman profile image
Kasey Speakman • Edited

My apologies on the name mixup Jochem. It was an auto-correct accident (in my brain instead of my smartphone), no disrespect intended.

Ok cool. This was the first I heard of disabling iframes, so I was curious also. I figure CodePen might do it for XSS-related reasons, considering the nature of their site. But I always like to understand the specific use cases for such things.

Thanks for the informative article.