简体   繁体   English

框架、脚本和加载顺序

[英]Frames, scripts, and load order

If I have a document with a frameset and some javascript defined in a head tag, ie something like this:如果我有一个带有框架集的文档,并且在 head 标签中定义了一些 javascript,例如:

<html>
 <head>
  <script>
     function foo() {}
  </script>   
 </head>
 <frameset>
    <frame src="A.html">
 </frameset>     
</html>

I am already assuming that the frame (A.html) fires its onLoad event handler before that of its parent.我已经假设框架(A.html)在其父级之前触发其 onLoad 事件处理程序。 I'm almost positive that is a safe assumption.我几乎肯定这是一个安全的假设。 However, is it safe to assume that the script context of A.html can safely access all of the script code defined in the head of the parent window?但是,假设 A.html 的脚本上下文可以安全地访问父 window 的头部中定义的所有脚本代码是否安全?

What assumptions can I make?我可以做出哪些假设?

According to this page , you can safely access the script in the parent , which is the object referring to a frame's parent frameset document.根据此页面,您可以安全地访问parent中的脚本,即 object 引用框架的父框架集文档。

From a little local test, the parent's onload event doesn't seem to fire , but the its script tag all seems to have been processed before the child frames are loaded.从一个小的本地测试来看,父母的 onload 事件似乎没有触发,但它的脚本标签似乎在加载子框架之前已经被处理了。

Framesets are frowned on these days, so be sure that it's the right approach.这些天框架集不受欢迎,因此请确保这是正确的方法。

The frame script will be able to access its parent JS space via the parent reference.框架脚本将能够通过parent引用访问其父 JS 空间。 (But only if the two documents are on the same host - which they are in your stripped-down example). (但前提是这两个文档位于同一主机上 - 它们在您的精简示例中)。

I wouldn't rely on parent's onLoad firing after A.html 's, though as Phil mentioned, the script will process first - this is because inline script evaluation is a blocking operation.我不会在A.html之后依赖父级的onLoad触发,尽管正如 Phil 提到的,脚本将首先处理 - 这是因为内联脚本评估是一个阻塞操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM