使用Javascript将HTML5沙盒属性添加到iframe
我有一个空的iframe和一个按钮:使用Javascript将HTML5沙盒属性添加到iframe,javascript,html,iframe,sandbox,Javascript,Html,Iframe,Sandbox,我有一个空的iframe和一个按钮: 但是(除.location.href外)我需要设置属性sandbox=“allow forms allow scripts allow same origin,因为框架站点“deframes”。如何设置位置和沙盒?如果你想在一次点击事件中做两件事。让一个函数同时做这两件事并在点击时启动该函数 window.onload = function(){ var button = document.getElementsByName("B1")[0]
但是(除.location.href外)我需要设置属性
sandbox=“allow forms allow scripts allow same origin
,因为框架站点“deframes”。如何设置位置和沙盒?如果你想在一次点击事件中做两件事。让一个函数同时做这两件事并在点击时启动该函数
window.onload = function(){
var button = document.getElementsByName("B1")[0]
var iframe = document.getElementsByName("IFrameName1")[0]
button.addEventListener('click',addSandboxAndChangeLocation,false);
function addSandboxAndChangeLocation(){
frames['IFrameName1'].location.href='https://www.google.com/';
iframe.sandbox = 'allow-forms allow-scripts allow-same-origin';
}
}
查看!虽然您可以将
iframe.sandbox
属性设置为字符串,但从技术上讲,它是一个接口,因此您还可以add()
和remove()
单个令牌:
let myIframe = document.createElement('iframe');
myIframe.sandbox.add('allow-scripts');
myIframe.sandbox.toggle('allow-forms');
console.log(myIframe.sandbox.toString())
// Returns: "allow-scripts allow-forms"
console.log(myIframe.outerHTML)
// Returns: <iframe sandbox="allow-scripts allow-forms"></iframe>
让myIframe=document.createElement('iframe');
添加('allow-scripts');
myIframe.sandbox.toggle('allow-forms');
console.log(myIframe.sandbox.toString())
//返回:“允许脚本允许表单”
log(myIframe.outerHTML)
//返回:
谢谢,我正在尝试..也许使用jQuery会更容易?使用jQuery肯定会更容易。恐怕沙盒不适用,它会继续碎片整理,而纯HTML iframe不会。我想我看到了我们的问题。我们必须在iframe dom元素上设置沙盒属性,而不是框架本身。当然,我看到它在JSFIDLE上工作。谢谢感谢您的帮助和时间;)