user013314
user013314

Reputation: 117

How can I automatically add allowfullscreen to every new iframe

I'm using codes like this in my blog:

<iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/0eBDVy5nihM?rel=0" frameborder="0" allowfullscreen></iframe>

And my question is simple, how can I automatically add allowfullscreen to every new iframe using javascript/jQuery?

Also, I presume the script should identify if there's the tag allowfullscreen added already.

I couldn't find a solution anywhere else.

Upvotes: 0

Views: 4474

Answers (2)

ayxos
ayxos

Reputation: 408

From here: https://stackoverflow.com/a/3774041/3764994 and here: Set IFrame allowfullscreen with javascript

To get all the iframe elements use getElementsByTagName(), then iterate over those with a for loop:

Something like this:

var i, frames;
frames = document.getElementsByTagName("iframe");
for (i = 0; i < frames.length; ++i)
{
      // The iFrame
    if (!frames[i].hasAttribute("allowfullscreen")) {
        frames[i].setAttribute('allowFullScreen', '')
    }
}

Upvotes: 3

meshin
meshin

Reputation: 478

Should be just this:

$('iframe').attr('allowFullScreen', '');

There is no need to check for allready set allowfullscreen as there will be no error if it is.

Upvotes: 4

Related Questions