首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用Javascript向iframe添加HTML5沙箱属性

使用Javascript向iframe添加HTML5沙箱属性
EN

Stack Overflow用户
提问于 2012-10-23 07:34:08
回答 2查看 11.8K关注 0票数 6

我有一个空的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”。如何设置位置和沙箱?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-10-23 07:41:57

如果你想在一次单击事件中做两件事。让一个函数同时做这两件事,并在单击时触发该函数。

代码语言:javascript
复制
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上查看它!

票数 5
EN

Stack Overflow用户

发布于 2018-09-16 02:48:49

虽然您可以将iframe.sandbox属性设置为字符串,但从技术上讲,它是一个DOMTokenList接口,因此您还可以使用add()remove()单令牌:

代码语言:javascript
复制
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>
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13021500

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档