JSFiddle - React, Tailwind, and code Playground
CSS
pre {
margin: 1em;
padding: 1em;
background: #f2f2f2;
border: 1px solid #aaa;
}
JavaScript
// 1. Setup
var frameElement = document.createElement( 'iframe' );
var frameWrapDiv = document.createElement( 'div' );
frameWrapDiv.appendChild(frameElement);
document.body.appendChild(frameWrapDiv);
var frameWin = frameElement.contentWindow;
var frameDoc = frameWin.document;
frameDoc.open();
frameDoc.write('<!doctype html><html><body><div>Hi</div></body></html>');
frameDoc.close();
var frameBody = frameDoc.body;
// 2. Hide
frameWrapDiv.style.display = 'none';
// 3a. jQuery.css
var computed,
dirJQ, dirGlobalGCS, dirOwnerDocGCS, dirJQPretend;
try {
// Read style from iframe'sjq <body>
// http://bugs.jquery.com/ticket/15098
// Worked in 1.8.3, broken in 1.11.1
dirJQ = $( frameBody ).css( 'direction' );
} catch (e) {
// Firefox throwing NS_ERROR_NOT_AVAILABLE?
dirJQ = e;
}
// 3b. Vanilla window.getComputedStyle
try {
computed = window.getComputedStyle( frameBody, null );
dirGlobalGCS = computed && {
sub: computed.direction,
call: computed.getPropertyValue('direction')
};
} catch (e) {
dirGlobalGCS = e;
}
// 3c. Vanilla node.ownerDocumenet.defaultView.getComputedStyle
try {
computed = frameBody.ownerDocument.defaultView.getComputedStyle( frameBody, null );
dirOwnerDocGCS = computed && {
sub: computed.direction,
call: computed.getPropertyValue('direction')
};
} catch (e) {
dirOwnerDocGCS = e;
}
// 3d. What jQuery does?
try {
computed = frameBody.ownerDocument.defaultView.getComputedStyle( frameBody, null );
dirJQPretend = computed ? (
computed.getPropertyValue('direction') || computed.direction
) : undefined;
} catch (e) {
dirJQPretend = e;
}
// Log
var pre = document.createElement('pre');
document.body.appendChild(pre);
function dump(x) {
if (typeof x === 'object' && x !== null) {
if (x.sub !== undefined) {
return '- sub: ' + dump(x.sub) + '\n- getPropertyValue(): ' + dump(x.call);
}
return x;
}
...