Add HTML5 sandbox attribute in iframe using Javascript

I have 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 frame site is "deframes". How to set location and sandbox?

+3
source share
1 answer

If you want to do two things per click event. The function does both of these things and fires when the button is pressed.

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 !

+3
source

All Articles