我有一个空的iframe和一个按钮:
<input type="button" name="B1" value="google" onclick="frames['IFrameName1'].location.href='https://www.google.com/'">
但是(除了.location.href)我需要设置属性sandbox="allow-forms allow-scripts allow-same-origin
,因为框架的站点是“deframe”。如何设置位置和沙箱?
发布于 2012-10-23 07:41:57
如果你想在一次单击事件中做两件事。让一个函数同时做这两件事,并在单击时触发该函数。
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';
}
}
在jsFiddle上查看它!
发布于 2018-09-16 02:48:49
虽然您可以将iframe.sandbox
属性设置为字符串,但从技术上讲,它是一个DOMTokenList接口,因此您还可以使用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>
https://stackoverflow.com/questions/13021500
复制相似问题