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フレーム化されたサイトが「デフレーム」するため、属性を設定する必要があります。場所とサンドボックスを設定する方法は?

4

2 に答える 2

6

iframe.sandboxプロパティを文字列として設定できますが、技術的にはDOMTokenListインターフェイスであるため、トークンadd()を1つだけ設定することもできます。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>
于 2018-09-15T18:48:48.737 に答える
5

ワンクリックイベントで2つのことをしたい場合。関数にこれらの両方を実行させ、クリックするとその関数を起動します。

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でチェックしてください!

于 2012-10-22T23:41:57.320 に答える