Skip to content

Instantly share code, notes, and snippets.

@barneycarroll
Last active July 13, 2021 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barneycarroll/9769319 to your computer and use it in GitHub Desktop.
Save barneycarroll/9769319 to your computer and use it in GitHub Desktop.
Attempt at detecting CSS `:target` support
// Determine whether or not we need to run fallback code by testing for `:target` support.
var targetSupport = ( function targetSupport(){
var iframe = document.createElement( 'iframe' );
var body = document.getElementsByTagName( 'body' )[ 0 ];
var doc;
var outcome;
iframe.tabindex = -1;
iframe.style.display = 'none';
body.appendChild( iframe );
doc = iframe.contentWindow.document;
doc.write( '<body id="x"><style>:target{display:none}</style>' );
doc.location.hash = 'x';
if( getComputedStyle(doc.getElementById( 'x' )).display === 'none' ){
outcome = true;
}
else {
outcome = false;
}
body.removeChild( iframe );
return outcome;
}() );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment