I'm trying to load iframe, and after it was loaded to inject to it my custom css style. But when I'm loading the page I see my css style appears for a split second and then the iframe finally loaded it overrides my style and it disappears.
I know, I need to wait until the iframe is loaded, and I tried to do so but without any success.
When I'm stopping debugger after execution of the following codeline, I can see the background changes. But then it disappears.
.append('<style> body{background:red;}</style>');
Look at the code snippet.
(function(){
var iFrame = $('iframe');
var contents = iFrame.contents();
var styleTag = contents.find('head').append('<style> body{background:red;}</style>');
})();
#my-iframe {
width: 100%;
height: 600px;
}
#middle-container {
width: 500px;
margin-left: auto;
margin-right: auto;
}
#iframe-container {
width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<title>iFrame Test Page</title>
</head>
<body>
<div id="middle-container">
<div id="iframe-container">
<iframe id="my-iframe" src="http://www.w3schools.com">
<p>Sorry! Your browser doesn't support iFrames</p>
</iframe>
</div>
</div>
</body>
</html>