Add HTML5 sandbox attribute to an iframe using Javascript

前端 未结 2 639
抹茶落季
抹茶落季 2021-01-05 21:47

I\'ve got an empty iframe and a button:

2条回答
  •  孤街浪徒
    2021-01-05 22:38

    While you can set the iframe.sandbox property as a string, it's technically a DOMTokenList interface, so you can also add() and remove() single tokens:

    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: 
    

提交回复
热议问题