在iframe加载之前运行脚本 [英] Run script before iframe loads

查看:112
本文介绍了在iframe加载之前运行脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用嵌入在应用程序中的帮助系统。



帮助的重要部分尝试使用相同的document.domain值,但是子iframe似乎在父进程之前运行其document.domain设置。这是一个问题,因为它会抛出安全错误并停止执行javascript。这里基本上是html的样子,给出正确的想法:

 < html> 
< head>
<! - 偶尔会跑第二个 - >
< script src =change-document-domain.js>< / script>
< / head>

< body>
< iframe>
< html>

< head>
<! - 偶尔先运行 - >
< script src =change-document-domain.js>< / script>
< / head>

< / html>
< / iframe>
< / body>
< / html>

帮助受到构建它的程序的限制。我可以对父脚本或父脚本标记进行任何更改以使其在iframe或其脚本之前加载吗?



如果您有任何疑问,请告诉我。 ,并且一如既往地谢谢!

解决方案

您可以做的一件事是在脚本加载后设置iFrame的来源。 / p>

因此,创建一个将在页面加载时加载脚本的函数,然后在完成脚本后,您可以设置一个回调函数,该函数将在脚本完成加载后执行。

  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 ===完成){
script.onreadystatechange = null;
callback();
}
};
} else {//其他
script.onload = function(){
callback();
};
}

script.src = url;
document.getElementsByTagName(head)[0] .appendChild(script);
}


//调用函数...
loadScript('change-document-domain.js',function(){
/ /脚本加密后执行。
document.getElementById(myFrame)。src =sourceOfIFrame.html
});


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!

解决方案

One 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 completed 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 === "complete" ) {
        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"
});

这篇关于在iframe加载之前运行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆