admin管理员组文章数量:1336201
I'm working with a help system which is embedded in an application.
The important part of the help tries to have the same document.domain value, but the child iframe seems to run its document.domain setting before the parent does. This is a problem because it throws a security error and halts javascript execution. Here's basically what the html looks like, to give the proper idea:
<html>
<head>
<!--Occasionally runs second-->
<script src="change-document-domain.js"></script>
</head>
<body>
<iframe>
<html>
<head>
<!--Occasionally runs first-->
<script src="change-document-domain.js"></script>
</head>
</html>
</iframe>
</body>
</html>
The help is very restricted by the program which builds it. Is there any change I can make to the parent script or parent script tag to make it load before the iframe or its script does?
Let me know if you have any questions, and thanks as always!
I'm working with a help system which is embedded in an application.
The important part of the help tries to have the same document.domain value, but the child iframe seems to run its document.domain setting before the parent does. This is a problem because it throws a security error and halts javascript execution. Here's basically what the html looks like, to give the proper idea:
<html>
<head>
<!--Occasionally runs second-->
<script src="change-document-domain.js"></script>
</head>
<body>
<iframe>
<html>
<head>
<!--Occasionally runs first-->
<script src="change-document-domain.js"></script>
</head>
</html>
</iframe>
</body>
</html>
The help is very restricted by the program which builds it. Is there any change I can make to the parent script or parent script tag to make it load before the iframe or its script does?
Let me know if you have any questions, and thanks as always!
Share Improve this question edited May 19, 2014 at 16:10 isherwood 61.2k16 gold badges121 silver badges169 bronze badges asked May 19, 2014 at 16:03 John WojtkJohn Wojtk 1252 silver badges10 bronze badges4 Answers
Reset to default 3One thing that you can do is set the source of the iFrame after the script loads.
So, create a function that will load the script on page load, and then once the scrip is pleted you can set a callback function which will execute after your script is done loading.
function loadScript( url, callback ) {
var script = document.createElement( "script" )
script.type = "text/javascript";
if(script.readyState) { //IE
script.onreadystatechange = function() {
if ( script.readyState === "loaded" || script.readyState === "plete" ) {
script.onreadystatechange = null;
callback();
}
};
} else { //Others
script.onload = function() {
callback();
};
}
script.src = url;
document.getElementsByTagName( "head" )[0].appendChild( script );
}
// call the function...
loadScript('change-document-domain.js', function() {
//executes after the script is laoded.
document.getElementById("myFrame").src = "sourceOfIFrame.html"
});
Not sure about whether you want this kind of solution or not. But using javascript or jquery for this purpose might be a workaround.
<iframe id="frame" src="">
</iframe>
And you just trigger the iFrame load after the parent content javascript is loaded ...
$("#frame").prop("src", "http://iframe-url./");
try creating a separate page containing the content for the iframe and like to it, which could be slightly slower, but would load in order.
Based on runcorn's answer (which did not do it for me), add a time delay (of one second) by wrapping in a function:
setTimeout(function() {
$("#frame").prop("src", "http://iframe-url./");;
}, 1000);
本文标签: javascriptRun script before iframe loadsStack Overflow
版权声明:本文标题:javascript - Run script before iframe loads - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742399942a2467685.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论