Search code examples
javascripthtmlgoogle-chromeiframesandbox

Detect support for HTML5 iframe sandbox attribute


Does anyone know how to detect if the iframe sandbox attribute is supported by the browser without relaying on version checks, etc?


Solution

  • You can check whether an iframe element has the sandbox attribute:

    var sandboxSupported = "sandbox" in document.createElement("iframe");
    

    Side note

    A good way to find feature detects is to look at Modernizr and see if it has one already. This is the Modernizr code for the sandbox attribute test:

    Modernizr.addTest('sandbox', 'sandbox' in document.createElement('iframe'));
    

    Alternatively (if you need to use lots of feature detects in your app) include Modernizr and use it properly, instead of just getting ideas from its source!