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!
Add a comment
|
2 Answers
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");
}
Comments
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