0

I have multiple video players on my site, but they open popup ads, so to block them I use the sandbox attribute, but not all of the players have the sandbox attribute. So I need javascript to automatically add the sandbox="allow-modals allow-orientation-lock allow-pointer-lock allow-presentation allow-scripts allow-top-navigation allow-forms attribute to all iframes on the page. How can I do this? Help would be highly appreciated! Thanks!

2 Answers 2

1

You can select all the frames in a page by using getElementsByTagName, loop over them, and set the attribute of a DOM element by using Element.setAttribute:

var frames = document.getElementsByTagName('iframe');
for (var frame of frames) {
    frame.setAttribute("sandbox", "allow-modals allow-orientation-lock allow-pointer-lock allow-presentation allow-scripts allow-top-navigation allow-forms");
}

jsfiddle example here

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

Comments

0

Try to use this JavaScript code:

const iframes = document.getElementsByTagName('iframe');

for (var i = 0; i < iframes.length; i++) {
    iframes[i].setAttribute('sandbox', 'allow-modals allow...');
}

Demo - https://codepen.io/vyspiansky/pen/poyjGPX?editors=0010

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.