I’m trying to inject a javascript into a iframe, but nothing works. I’ve already spent days with this issue, would be great if someone could give me some hints.
Example of subject:
The example.html:
<html>
<head>
</head>
<body>
<iframe src="http://example.com/test.html" id="myFrame">
</iframe>
</body>
</html>
The test.html:
<html>
<head>
</head>
<body background="./bg.jpg">
<p >Text </p>
<p >(<i>Something</i>)<br><img src="./img1.gif" align="left">Text</p>´
</body>
</html>
Desired Output from test.html:
<html>
<head>
<script type="text/javascript"> src="myscript.js</script>
</head>
<body background="./bg.jpg">
<p >Text </p>
<p >(<i>Something</i>)<br><img src="./img1.gif" align="left">Text</p>´
</body>
</html>
I’ve tried this, but it doesn’t work:
The example.html:
<html>
<head>
<script type="text/javascript">
var rawframe = document.getElementById('myFrame');
var doc = rawframe.contentDocument;
if (!doc && rawframe.contentWindow) {
doc = rawframe.contentWindow.document;
}
var script = doc.createElement('script');
script.type = "text/javascript";
script.src = "myscript.js";
document.body.appendChild(script);
</script>
</head>
<body>
<iframe src="http://example.com/test.html" id="myFrame">
</iframe>
</body>
</html>
Thank you all for the help.
Unfortunately, I don’t believe this is possible. I know I tried something along these lines before, but due to security reasons, browsers prevented it.