Skip to content

Instantly share code, notes, and snippets.

@make-sum
Last active December 24, 2015 07:59
Show Gist options
  • Save make-sum/6767747 to your computer and use it in GitHub Desktop.
Save make-sum/6767747 to your computer and use it in GitHub Desktop.
Get iframe by ID and append a CSS style to affect an element inside the iframe.
var iframe1 = document.getElementById('myiframeID');
iframe1.onload = function (){
if(this.contentDocument) iframe1.doc = iframe1.contentDocument;
else if(iframe.contentWindow) iframe1.doc = iframe1.contentWindow.document;
var css = '.myelement{float:right;}',
header = iframe1.doc.getElementsByTagName('head')[0],
style = document.createElement('style');
style.type = 'text/css';
if (style.styleSheet){
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
header.appendChild(style);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment