开发者

JS function execution order

开发者 https://www.devze.com 2023-02-09 17:50 出处:网络
I have two pages namely Parent.xhtml(controls the layout) and Child.xhtml(displays the content). Child page is included in the Parent page by using <iframe> tag.

I have two pages namely Parent.xhtml(controls the layout) and Child.xhtml(displays the content). Child page is included in the Parent page by using <iframe> tag.

I need to implement an onload functionality by using javascript. Before that, I want to know in which order the javascript f开发者_如何转开发unctions will execute?

Will the Parent page js function execute first? or Child page js function will execute first?

Awaiting your answers.! Thanks in advance


According to a previous answer, browsers might not wait for iframes to load before firing the onload event on the parent window. You can't make assumptions on which one will complete first - there's the possibility of a race condition going on there, so either one could finish first.

One solution might be to write a function that waits until it's been called a certain number of times before executing:

var runs = 0;
function onLoad() {
    if (++runs < 2) return;
    // put code to execute once both have loaded
}

Then in HTML:

<body onload="onLoad();">
    .....
    <iframe onload="onLoad();">

Alternatively, set the URL of the iframe in the onload handler of the parent window.

0

精彩评论

暂无评论...
验证码 换一张
取 消