Javascript DOM HTML IFrame sandbox Property

Introduction

The sandbox property returns the value of the sandbox attribute in an iframe element.

Return the value of the sandbox attribute:

var x = document.getElementById("myFrame").sandbox;

Click the button to display the value of the sandbox attribute.

View in separate window

<!DOCTYPE html>
<html>
<body>

<p>The "Get date and time" button will run a script in the inline frame.</p>

<iframe id="myFrame" src="https://www.java2s.com" sandbox="allow-scripts">
  <p>Your browser does not support iframes.</p>
</iframe>// w  w  w  .ja v a2 s  .  co  m
<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {
  var x = document.getElementById("myFrame").sandbox;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The sandbox attribute enables security restrictions for iframe with untrusted content.

The value of the sandbox attribute can either be

  • an empty string, which means that all the restrictions is applied
  • a space-separated list of pre-defined values that will REMOVE particular restrictions.

This property is read-only.

The sandbox property returns a String representing the value of the sandbox attribute.




PreviousNext

Related