7

I've got an empty iframe and a button:

<input type="button" name="B1" value="google" onclick="frames['IFrameName1'].location.href='https://www.google.com/'">

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?

2 Answers 2

11

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: <iframe sandbox="allow-scripts allow-forms"></iframe>
Sign up to request clarification or add additional context in comments.

1 Comment

Note: jsdom doesnt support .sandbox as for now. see github.com/jsdom/jsdom/issues/1892 stackoverflow.com/questions/25387977/…
5

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';
    }
} 

Check it out on jsFiddle!

11 Comments

Thanks, i am trying.. May be it'd be easier with jQuery?
It would be easier with jQuery for sure.
i'm afraid sandbox did not apply and it goes on deframing, while pure HTML iframe does not.
I think I see our problem. We have to set the sandbox attribute on the iframe dom element not the frame itself.
Sure, i see it is working on jsFiddle. Thank you for your help and time ;)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.