I have this code to add a stylesheet to a page in a fancybox iframe. Can I do the same to the parent page?
I have tried this but it didn’t work…
window.parent.changeStyles(stylesheet);
function changeStyles(styles) {
$(“”, {
rel: “stylesheet”,
type: “text/css”,
href: styles
}).appendTo(“head”);
};
$('.change-styles').on('click',function(){
var stylesheet = $(this).attr('href');
changeStyles(stylesheet);
return false
});
First, define a parent file:
Then the iframe file as
iframe.html:The
addCSSToParent()will add a new DOM element into the head of its parent with the given URL. Make sure to run this in a server environment, because accessing the parent worked but gave a few security errors in Chrome.