使用Javascript将HTML5沙箱属性添加到iframe [英] Add HTML5 sandbox attribute to an iframe using Javascript

查看:93
本文介绍了使用Javascript将HTML5沙箱属性添加到iframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个空的iframe和一个按钮:

I've got an empty iframe and a button:

< input type =buttonname =B1value = google
onclick =frames ['IFrameName1'] .location.href ='https://www.google.com/'>

但是(除了.location.href)我需要设置属性 sandbox =allow-forms allow-scripts allow-same-origin ,因为框架网站deframes。如何设置位置和沙箱?

But (besides .location.href) i need to set the attribute sandbox="allow-forms allow-scripts allow-same-origin, because the framed site "deframes". How to set location AND sandbox?

推荐答案

如果你想在一次点击中做两件事有一个函数同时执行这两个操作并在点击时激活该函数。

If you want to do two things on one click event. Have a function do both of those things and fire that function on click.

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

这篇关于使用Javascript将HTML5沙箱属性添加到iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆